C# 处理图片
2012-05-07 09:31
197 查看
今天的工作是把服务器上所有的电影封面图片稍微裁剪,适当缩放。贴下查资料和自己用到的代码~~
用到的一个枚举
1、按比例缩放图小
2、缩放到指定大小
3、裁剪缩放
4、四面裁剪图片
5、忽略比列直接缩放
用到的一个枚举
enum AnchorPosition { Top, Center, Bottom, Left, Right }
1、按比例缩放图小
static Image ScaleByPercent(Image imgPhoto, int Percent) { float nPercent = ((float)Percent/100); int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); 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(destX,destY,destWidth,destHeight), new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; }
2、缩放到指定大小
static Image FixedSize(Image imgPhoto, int Width, int Height) { int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; nPercentW = ((float)Width/(float)sourceWidth); nPercentH = ((float)Height/(float)sourceHeight); if(nPercentH < nPercentW) { nPercent = nPercentH; destX = System.Convert.ToInt16((Width - (sourceWidth * nPercent))/2); } else { nPercent = nPercentW; destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent))/2); } int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.Clear(Color.Red); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto, new Rectangle(destX,destY,destWidth,destHeight), new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; }
3、裁剪缩放
static Image Crop(Image imgPhoto, int Width, int Height, AnchorPosition Anchor) { int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; nPercentW = ((float)Width/(float)sourceWidth); nPercentH = ((float)Height/(float)sourceHeight); if(nPercentH < nPercentW) { nPercent = nPercentW; switch(Anchor) { case AnchorPosition.Top: destY = 0; break; case AnchorPosition.Bottom: destY = (int) (Height - (sourceHeight * nPercent)); break; default: destY = (int) ((Height - (sourceHeight * nPercent))/2); break; } } else { nPercent = nPercentH; switch(Anchor) { case AnchorPosition.Left: destX = 0; break; case AnchorPosition.Right: destX = (int) (Width - (sourceWidth * nPercent)); break; default: destX = (int) ((Width - (sourceWidth * nPercent))/2); break; } } int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto, new Rectangle(destX,destY,destWidth,destHeight), new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; }
4、四面裁剪图片
static Image CutABit(Image imgPhoto, int topToCut, int bottomToCut, int lefToCut, int rightToCut) { int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; Bitmap bmPhoto = new Bitmap(sourceWidth, sourceHeight - 60, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, sourceWidth - lefToCut - rightToCut, sourceHeight - topToCut - bottomToCut), new Rectangle(sourceX + lefToCut, sourceY + topToCut, sourceWidth - lefToCut - rightToCut, sourceHeight - topToCut - bottomToCut), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; }
5、忽略比列直接缩放
static Image Resize(Image imgPhoto, int Width, int Height) { int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.Clear(Color.White); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, Width, Height), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; }
相关文章推荐
- C# 后台处理图片的几种方法
- 在C#中使用WIA获取扫描仪数据(利用Filter处理图片)
- C#开发——简单的图片处理方法(更新中)
- (转)打印相关_C#图片处理Bitmap位图缩放和剪裁
- C# 图片处理 将 图片数组将图片缩放为指定大小
- C#图片处理之: 锐化
- C#.Net 图片处理大全
- C# 后台处理图片的几种方式
- C#中处理图片的拼接
- C#和NPOI处理中的EXCEL中的图片
- C# 图片处理之:彩色图片转为黑白图
- C#图片处理示例(裁剪,缩放,清晰度,水印)
- 转:C#图片处理类(裁剪,缩放,清晰度,水印)
- C#图片处理示例(裁剪,缩放,清晰度,水印)
- C# 实现 HTML+一般处理程序图片上传功能如图!
- C#图片灰度处理(位深度24→位深度8)
- C#图像处理(5):无损保存图片
- 黄聪:C#图片处理封装类(裁剪、缩放、清晰度、加水印、生成缩略图)有示例(转)
- c#怎么把一个彩色的图片处理成灰色的呢。