解决方案 »
- OracleDataAdapter的奇怪问题
- powertalk 在线聊天工具传文件问题?
- SOS: 如何调用远程机器上的Bat文件
- timer控件的第一次激活的时间问题?
- 如何得到app.config中的信息?
- OpenFileDialog如何不关闭 比如当打开的文件名不是我想要得时候
- 『控件发布』发布一款很好用的TreeList控件,欢迎使用!
- 怎么样在用C#生成的Excel中实现标题置顶,即实现拆分和窗口冻结的功能?
- 有没有人用C#处理视频
- mfc,.net,vcl,j2xe,这些平台都用过,有需要考虑内存的有不需要考虑内存,那,内存的管理到底是不是程序员该考虑的呢。。。。。???
- 请问CS项目里面这个菜单怎么实现
- mysql数据库连接莫名奇妙的关闭
public static bool CropImage(string photoName,int x,int y,int width,int height)
{
System.Drawing.Image image = null;
System.Drawing.Image image_TM = null;
System.Drawing.Image image_Temp = null;
System.Drawing.Image image_TS = null;
try
{
string filePath =System.Web.HttpContext.Current.Server.MapPath(photoName); string ext = System.IO.Path.GetExtension(filePath).ToLower(); image = System.Drawing.Image.FromFile(filePath);
image_Temp = ImageResize.Crop(image,x,y,width,height);
image_TS = ImageResize.FixedSize(image_Temp,AlbumConstants.Thumbnail_S_Max,AlbumConstants.Thumbnail_S_Mix,true);
image_TM = ImageResize.FixedSize(image_Temp,AlbumConstants.Thumbnail_M_Max,AlbumConstants.Thumbnail_M_Min,false); System.Drawing.Imaging.ImageFormat imgf = System.Drawing.Imaging.ImageFormat.Bmp;
switch (ext)
{
case ".bmp":
ext = ".bmp";
imgf = System.Drawing.Imaging.ImageFormat.Bmp;
break;
case ".jpg":
ext = ".jpg";
imgf = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case ".jpeg":
ext = ".jpeg";
imgf = System.Drawing.Imaging.ImageFormat.Jpeg;
break;
case ".png":
ext = ".png";
imgf = System.Drawing.Imaging.ImageFormat.Png;
break;
case ".gif":
ext = ".gif";
imgf = System.Drawing.Imaging.ImageFormat.Gif;
break;
default:
ext = ".bmp";
imgf = System.Drawing.Imaging.ImageFormat.Bmp;
break;
}
image.Dispose();
string strFileName_TS = filePath.Replace(AlbumConstants.SymbolOfThumbnailImage_M,AlbumConstants.SymbolOfThumbnailImage_S);
image_TS.Save(strFileName_TS,imgf);
image_TM.Save(filePath,imgf);
}
catch(Exception error)
{
throw error;
}
finally
{
if(image!= null)
image.Dispose();
if(image_TM!= null)
image_TM.Dispose();
if(image_TS!= null)
image_TS.Dispose();
if(image_Temp !=null)
image_Temp.Dispose();
}
return true;
}
#endregion 剪切图片
{
int sourceWidth = imgPhoto.Width;//原图片的宽
int sourceHeight = imgPhoto.Height;//原图片的高 int ProcessWidth = 0;//处理图片的宽
int ProcessHeight = 0;//处理图片的高 int destWidth = 0;//剪切时的宽
int destHeight = 0;//剪切时的高 int sourceX = 0;
int sourceY = 0; int destX = 0;//剪切时x坐标
int destY = 0;//剪切时y坐标 float nPercent = 0; if(sourceWidth > sourceHeight)//计算处理图片的宽和高
{
ProcessWidth = AlbumConstants.ProcessImg_Width;
nPercent = ((float)AlbumConstants.ProcessImg_Width/(float)sourceWidth);
ProcessHeight = (int)(sourceHeight * nPercent);
}
else
{
ProcessHeight = AlbumConstants.ProcessImg_Width;
nPercent = ((float)AlbumConstants.ProcessImg_Width/(float)sourceHeight);
ProcessWidth = (int)(sourceWidth * nPercent);
} nPercent = ((float)sourceWidth/(float)ProcessWidth);//计算原图x坐标
destX = (int)(nPercent * x); nPercent = ((float)sourceHeight/(float)ProcessHeight);//计算原图x坐标
destY = (int)(nPercent * y); nPercent = ((float)sourceHeight/(float)ProcessHeight);//计算原图高
destHeight = (int)(nPercent * height); nPercent = ((float)sourceWidth/(float)ProcessWidth);//计算原图宽
destWidth = (int)(nPercent * width);
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto,
new Rectangle(sourceX,sourceY,destWidth,destHeight),
new Rectangle(destX,destY,destWidth,destHeight),
GraphicsUnit.Pixel); grPhoto.Dispose();
return bmPhoto; }
如何用这个实现的?需要操做的图片内容很多……
Graphics.DrawImage 方法 (Image, Point[], Rectangle, GraphicsUnit)
msdn上的描述也有部分错误的, 害我一个一个试。