http://www.codeproject.com/csharp/dotnet_convertimage.aspImage Format Conversion in .NET
解决方案 »
- 根据输入的姓名获取对应的英文/去除编辑器的格式
- 百分求 在MSDN的Webcast 中心的ORM教程地址 100 100
- 郁闷中的超时问题~~~~烦死了。。。。
- 为什么我的ASP.NET的TEXTBOX控件无法触发TextChanged事件?
- 想做好一个网站,你不得不面对的问题。关于数据库连接。
- 怎么在oracle 中插入 ’
- 关于global.asax的问题。。。。。项者有分
- 悬赏帖-EXTJS-WINDOW控件内实现图片自动放大功能
- 请教dataset问题!
- 有兴趣难题哦!!!高能都请进来看看如何办????
- DataGrid 中行的问题
- 如何使 TextBox 获取焦点时,TextBox 内的文本被全选?
using System;
using System.Web.Services;
using System.Xml.Serialization;
using System.Drawing; // for Image class
using System.Drawing.Imaging; // for ImageFormat class
using System.IO; // for FileStream class
public class WSC_ConvertImage : WebService {
[WebMethod]
public bool ConvertImage(byte[] bytInFile, int intToFormat,
out byte[] bytOutFile)
{
// has something been sent or not...
bytOutFile=null;
if (bytInFile.Length==0)
{
// nope.. indicate failure
return false;
} // Since webservices are stateless, and each webmethod call is
// indepedent of another, we must have a unique file name for
// processing each request.
string strFileName=Server.MapPath(".")+"\\"+
Guid.NewGuid().ToString(); // write the byte array sent to us as a file..
FileStream fsFile=null;
try
{
fsFile=File.Create(strFileName);
}
catch
{
// unable to create input file..
return false;
} // write the byte array to it..
try
{
fsFile.Write(bytInFile,0,bytInFile.Length);
}
catch
{
// unable to write to the file..
fsFile.Close();
return false;
} // close the file..
fsFile.Close(); // load the image from the file..
Image imgInFile=Image.FromFile(strFileName); // save to the format specified..
string strOutFileName=strFileName; switch(intToFormat)
{
case 1: // BMP
strOutFileName=strOutFileName+".BMP";
imgInFile.Save(strOutFileName,ImageFormat.Bmp);
break;
case 2: // EXIF
strOutFileName=strOutFileName+".EXIF";
imgInFile.Save(strOutFileName,ImageFormat.Exif);
break;
case 3: // EMF
strOutFileName=strOutFileName+".EMF";
imgInFile.Save(strOutFileName,ImageFormat.Emf);
break;
case 4: // GIF
strOutFileName=strOutFileName+".GIF";
imgInFile.Save(strOutFileName,ImageFormat.Gif);
break;
case 5: // ICO
strOutFileName=strOutFileName+".ICO";
imgInFile.Save(strOutFileName,ImageFormat.Icon);
break;
case 6: // JPEG
strOutFileName=strOutFileName+".JPG";
imgInFile.Save(strOutFileName,ImageFormat.Jpeg);
break;
case 7: // PNG
strOutFileName=strOutFileName+".PNG";
imgInFile.Save(strOutFileName,ImageFormat.Png);
break;
case 8: // TIFF
strOutFileName=strOutFileName+".TIFF";
imgInFile.Save(strOutFileName,ImageFormat.Tiff);
break;
case 9: // WMF
strOutFileName=strOutFileName+".WMF";
imgInFile.Save(strOutFileName,ImageFormat.Wmf);
break;
default:
strOutFileName=strOutFileName+".BMP";
imgInFile.Save(strOutFileName,ImageFormat.Bmp);
break;
} // read the output file..
try
{
fsFile=File.Open(strOutFileName,FileMode.Open,FileAccess.Read);
}
catch
{
// unable to read output file..
return false;
} // write to the output byte array..
try
{
// create array to read in image file..
int iSize=Convert.ToInt32(fsFile.Length);
bytOutFile = new byte[iSize]; // read the converted image...
fsFile.Read(bytOutFile,0,iSize);
}
catch
{
// unable to write to the array..
fsFile.Close();
return false;
} // close the file..
fsFile.Close(); // delete the created files..
try
{
File.Delete(strFileName);
File.Delete(strOutFileName);
}
catch
{
// do nothing..
} return true;
}
}