图片操作:生成缩略图、添加水印、截取图片等
来源:互联网 发布:linux入门教程视频 编辑:程序博客网 时间:2024/05/01 14:55
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Web;
-
- namespace XXXX.Common
- {
-
-
-
- public class ImagesHelper
- {
-
-
-
-
-
-
-
-
- public static int getImgWidth(Stream stream)
- {
- Image img = Image.FromStream(stream);
- int result = img.Width;
- img.Dispose();
- stream.Dispose();
- return result;
- }
-
-
-
-
-
-
- public static int getImgWidth(string filePath)
- {
- Image img = Image.FromFile(filePath);
- int result = img.Width;
- img.Dispose();
- return result;
- }
-
-
- #region 从文件流生成缩略图
-
-
-
-
-
-
-
-
-
- public static bool GetThumbNail(Stream stream, string savePath, int width, int height, ThumbNailScale scale)
- {
-
- Image img = Image.FromStream(stream);
- stream.Dispose();
- int towidth = width;
- int toheight = height;
- int x = 0;
- int y = 0;
- int ow = img.Width;
- int oh = img.Height;
-
- if (ow < width)
- width = ow;
-
- if (oh < height)
- height = oh;
-
-
- switch (scale)
- {
- case ThumbNailScale.Appointed:
- break;
- case ThumbNailScale.ScaleWidth:
- toheight = img.Height * width / img.Width;
- break;
- case ThumbNailScale.ScaleHeight:
- towidth = img.Width * height / img.Height;
- break;
- case ThumbNailScale.Cut:
- if ((double)img.Width / (double)img.Height > (double)towidth / (double)toheight)
- {
- oh = img.Height;
- ow = img.Height * towidth / toheight;
- y = 0;
- x = (img.Width - ow) / 2;
- }
- else
- {
- ow = img.Width;
- oh = img.Width * height / towidth;
- x = 0;
- y = (img.Height - oh) / 2;
- }
- break;
- case ThumbNailScale.ScaleDown:
- double Tw, Th;
- Tw = width;
- Th = height * (Convert.ToDouble(oh) / Convert.ToDouble(ow));
- if (Th > height)
- {
- Th = height;
- Tw = width * (Convert.ToDouble(ow) / Convert.ToDouble(oh));
- }
- towidth = Convert.ToInt32(Tw);
- toheight = Convert.ToInt32(Th);
- break;
- default:
- break;
- }
-
-
- Image bitmap = new Bitmap(towidth, toheight);
-
-
- Graphics g = Graphics.FromImage(bitmap);
-
-
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
-
-
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
-
-
- g.Clear(Color.Transparent);
-
-
-
- g.DrawImage(img, new Rectangle(0, 0, towidth, toheight),
- new Rectangle(x, y, ow, oh),
- GraphicsUnit.Pixel);
-
- try
- {
-
- bitmap.Save(savePath, ImageFormat.Jpeg);
- }
- catch (Exception ex)
- {
- Logger.Write(string.Format("从文件流生成缩略图:{0}", ex.Message), Logger.MsgType.Error);
- return false;
- }
- finally
- {
- img.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
-
- return true;
- }
- #endregion
-
- #region 从文件路径生成缩略图
-
-
-
-
-
-
-
-
-
- public static bool GetThumbNail(string originalImagePath, string savePath, int width, int height, ThumbNailScale scale)
- {
-
- Image img = Image.FromFile(originalImagePath);
-
- int towidth = width;
- int toheight = height;
-
- int x = 0;
- int y = 0;
- int ow = img.Width;
- int oh = img.Height;
-
-
- if (ow < width)
- width = ow;
-
- switch (scale)
- {
- case ThumbNailScale.Appointed:
- break;
- case ThumbNailScale.ScaleWidth:
- toheight = img.Height * width / img.Width;
- break;
- case ThumbNailScale.ScaleHeight:
- towidth = img.Width * height / img.Height;
- break;
- case ThumbNailScale.Cut:
- if ((double)img.Width / (double)img.Height > (double)towidth / (double)toheight)
- {
- oh = img.Height;
- ow = img.Height * towidth / toheight;
- y = 0;
- x = (img.Width - ow) / 2;
- }
- else
- {
- ow = img.Width;
- oh = img.Width * height / towidth;
- x = 0;
- y = (img.Height - oh) / 2;
- }
- break;
- case ThumbNailScale.ScaleDown:
- double Tw, Th;
- Tw = width;
- Th = height * (Convert.ToDouble(oh) / Convert.ToDouble(ow));
- if (Th > height)
- {
- Th = height;
- Tw = width * (Convert.ToDouble(ow) / Convert.ToDouble(oh));
- }
- towidth = Convert.ToInt32(Tw);
- toheight = Convert.ToInt32(Th);
- break;
- default:
- break;
- }
-
-
- Image bitmap = new Bitmap(towidth, toheight);
-
-
- Graphics g = Graphics.FromImage(bitmap);
-
-
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
-
-
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
-
-
- g.Clear(Color.White);
-
-
- g.DrawImage(img, new Rectangle(0, 0, towidth, toheight),
- new Rectangle(x, y, ow, oh),
- GraphicsUnit.Pixel);
-
- try
- {
-
- bitmap.Save(savePath, ImageFormat.Jpeg);
- }
- catch (Exception e)
- {
- throw e;
- }
- finally
- {
- img.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
-
- return true;
-
- }
- #endregion
-
- #region 获取图片格式
-
-
-
-
-
- public static ImageFormat GetImageType(object strContentType)
- {
- if ((strContentType.ToString().ToLower()) == "image/pjpeg")
- {
- return ImageFormat.Jpeg;
- }
- else if ((strContentType.ToString().ToLower()) == "image/gif")
- {
- return ImageFormat.Gif;
- }
- else if ((strContentType.ToString().ToLower()) == "image/bmp")
- {
- return ImageFormat.Bmp;
- }
- else if ((strContentType.ToString().ToLower()) == "image/tiff")
- {
- return ImageFormat.Tiff;
- }
- else if ((strContentType.ToString().ToLower()) == "image/x-icon")
- {
- return ImageFormat.Icon;
- }
- else if ((strContentType.ToString().ToLower()) == "image/x-png")
- {
- return ImageFormat.Png;
- }
- else if ((strContentType.ToString().ToLower()) == "image/x-emf")
- {
- return ImageFormat.Emf;
- }
- else if ((strContentType.ToString().ToLower()) == "image/x-exif")
- {
- return ImageFormat.Exif;
- }
- else if ((strContentType.ToString().ToLower()) == "image/x-wmf")
- {
- return ImageFormat.Wmf;
- }
- else
- {
- return ImageFormat.MemoryBmp;
- }
- }
- #endregion
-
-
-
-
-
-
-
- public static bool MakeWaterImage(Stream sourceFile, string saveFile)
- {
- bool result = false;
-
- try
- {
- Image imgPhoto = Image.FromStream(sourceFile);
- sourceFile.Close();
- sourceFile.Dispose();
-
- int phWidth = imgPhoto.Width;
- int phHeight = imgPhoto.Height;
-
- Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
- bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
-
- Image imgWatermark = new Bitmap(System.Web.HttpContext.Current.Server.MapPath("/images/watermark.png"));
- int wmWidth = imgWatermark.Width;
- int wmHeight = imgWatermark.Height;
-
- if (phWidth > (wmWidth + 100) && phHeight > (wmHeight + 100))
- {
- Graphics grPhoto = Graphics.FromImage(bmPhoto);
- grPhoto.Clear(Color.White);
- grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel);
-
- grPhoto.Dispose();
-
-
-
- using (Bitmap bmWatermark = new Bitmap(bmPhoto))
- {
- bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
- Graphics grWatermark = Graphics.FromImage(bmWatermark);
- using (ImageAttributes imageAttributes = new ImageAttributes())
- {
-
-
-
-
-
- float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } };
- ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
- imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
- int xPosOfWm = ((phWidth - wmWidth) - 2);
- int yPosOfWm = ((phHeight - wmHeight) - 2);
- grWatermark.DrawImage(imgWatermark, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes);
- }
- imgPhoto = bmWatermark;
- grWatermark.Dispose();
- imgPhoto.Save(saveFile, ImageFormat.Jpeg);
- }
-
- result = true;
- }
- else
- {
- result = false;
- }
- imgWatermark.Dispose();
- bmPhoto.Dispose();
- imgPhoto.Dispose();
- }
- catch(Exception ex)
- {
- Logger.Write(string.Format("生成水印图片错误:{0}",ex.Message), Logger.MsgType.Information);
-
- try
- {
- Image imgPhoto2 = Image.FromStream(sourceFile);
- imgPhoto2.Save(saveFile, ImageFormat.Jpeg);
- imgPhoto2.Dispose();
- result = true;
- }
- catch
- {
- result = false;
- }
- }
-
- return result;
-
- }
-
-
-
-
-
-
-
-
- public static bool MakeWaterImage(Stream sourceFile, string saveFile, ImagePosition Position)
- {
- bool result = false;
-
- try
- {
- Image imgPhoto = Image.FromStream(sourceFile);
-
-
- int phWidth = imgPhoto.Width;
- int phHeight = imgPhoto.Height;
-
- Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
- bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
-
- Image imgWatermark = new Bitmap(System.Web.HttpContext.Current.Server.MapPath("/images/watermark.png"));
- int wmWidth = imgWatermark.Width;
- int wmHeight = imgWatermark.Height;
-
- if (phWidth > (wmWidth + 100) && phHeight > (wmHeight + 100))
- {
- Graphics grPhoto = Graphics.FromImage(bmPhoto);
- grPhoto.Clear(Color.White);
- grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel);
-
- grPhoto.Dispose();
-
-
-
- using (Bitmap bmWatermark = new Bitmap(bmPhoto))
- {
- bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
- Graphics grWatermark = Graphics.FromImage(bmWatermark);
- using (ImageAttributes imageAttributes = new ImageAttributes())
- {
-
-
-
-
-
- float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } };
- ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
- imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
- int xPosOfWm = 0;
- int yPosOfWm = 0;
- switch (Position)
- {
- case ImagePosition.BottomRight:
- xPosOfWm = ((phWidth - wmWidth) - 2);
- yPosOfWm = ((phHeight - wmHeight) - 2);
- break;
- case ImagePosition.TopLeft:
- xPosOfWm = 2;
- yPosOfWm = 2;
- break;
- case ImagePosition.TopRigth:
- xPosOfWm = ((phWidth - wmWidth) - 2);
- yPosOfWm = 2;
- break;
- case ImagePosition.BottomLeft:
- xPosOfWm = 2;
- yPosOfWm = ((phHeight - wmHeight) - 2);
- break;
- case ImagePosition.Center:
- xPosOfWm = ((phWidth / 2) - (wmWidth / 2));
- yPosOfWm = ((phHeight / 2) - (wmHeight / 2));
- break;
- }
- grWatermark.DrawImage(imgWatermark, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes);
- }
- imgPhoto = bmWatermark;
- grWatermark.Dispose();
- imgPhoto.Save(saveFile, ImageFormat.Jpeg);
- }
-
- result = true;
- }
- else
- {
- Image imgPhoto2 = Image.FromStream(sourceFile);
- imgPhoto2.Save(saveFile, ImageFormat.Jpeg);
- imgPhoto2.Dispose();
- result = true;
- }
- imgWatermark.Dispose();
- bmPhoto.Dispose();
- imgPhoto.Dispose();
- }
- catch
- {
-
- try
- {
- Image imgPhoto2 = Image.FromStream(sourceFile);
- imgPhoto2.Save(saveFile, ImageFormat.Jpeg);
- imgPhoto2.Dispose();
- result = true;
- }
- catch
- {
- result = false;
- }
- }
-
- sourceFile.Close();
- sourceFile.Dispose();
-
- return result;
-
- }
-
- #region 从图片中截取一张指定大小的图片
-
-
-
-
-
-
-
-
-
- public static void CaptureImage(string sFromFilePath, string saveFilePath, int width, int height, int spaceX, int spaceY)
- {
-
- Image fromImage = Image.FromFile(sFromFilePath);
- int x = 0;
- int y = 0;
-
-
-
-
- int sX = fromImage.Width - width;
- int sY = fromImage.Height - height;
- if (sX > 0)
- {
- x = sX > spaceX ? spaceX : sX;
- }
- else
- {
- width = fromImage.Width;
- }
- if (sY > 0)
- {
- y = sY > spaceY ? spaceY : sY;
- }
- else
- {
- height = fromImage.Height;
- }
-
-
- Bitmap bitmap = new Bitmap(width, height);
-
- Graphics graphic = Graphics.FromImage(bitmap);
-
- graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
-
- Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
-
- saveImage.Save(saveFilePath, ImageFormat.Jpeg);
-
- saveImage.Dispose();
- bitmap.Dispose();
- graphic.Dispose();
- }
- #endregion
-
- public enum ImagePosition
- {
-
-
-
- Center,
-
-
-
- TopLeft,
-
-
-
- BottomLeft,
-
-
-
- BottomRight,
-
-
-
- TopRigth
- }
-
-
-
-
- public enum ThumbNailScale
- {
-
-
-
- Appointed,
-
-
-
- ScaleWidth,
-
-
-
- ScaleHeight,
-
-
-
- Cut,
-
-
-
- ScaleDown
- }
- }
- }