public MyGridImageEditControl( ImageList imageList, ImageSizeMode sizeMode) { ImageList = imageList; ImageSizeMode = sizeMode; ImageSizeMode = sizeMode; }
public ImageSize(ImageSizeMode mode, float?percent) { Mode = mode; Percent = percent; Width = 0; Height = 0; }
public ImageSize(ImageSizeMode mode) { Mode = mode; Percent = 0; Width = 0; Height = 0; }
public void Load(XmlDocument doc) { XmlNode n; ImageSizeMode sz; int ivalue; //timer n = doc.SelectSingleNode("/Settings/Slideshow/Timer"); if (n != null && int.TryParse(n.InnerText, out ivalue) && ivalue >= 0 && ivalue <= MAXIMUM_SLIDESHOW_TIMER) { Timer = ivalue; } else { Timer = DEFAULT_SLIDESHOW_TIMER; } //size mode for slideshow n = doc.SelectSingleNode("/Settings/Slideshow/SizeMode"); if (n != null && Enum.TryParse(n.InnerText, out sz)) { SizeMode = sz; } else { SizeMode = ConfigSlideshow.DEFAULT_IMAGESIZE_MODE; } }
public ImageSize(ImageSizeMode mode, float?percent, int width, int height) { Mode = mode; Percent = percent; Width = width; Height = height; }
/// <summary> /// 重新绘制指定尺寸的图片 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="graphicsHandler">对缩略图处理</param> public static byte[] DrawBySize( Image original, ImageSizeMode mode, int width, int height, ImageGraphicsHandler graphicsHandler) { return(DrawBySize(original, mode, width, height, ImageFormat.Jpeg, 100L, 60L, graphicsHandler)); }
/// <summary> /// Sets a new size mode for the given GridImageEditControl /// </summary> /// <param name="ic"></param> /// <param name="sizeMode"></param> private void SetNewSizeMode( GridImageEditControl ic, ImageSizeMode sizeMode) { if (ic != null) { ic.ImageSizeMode = sizeMode; } }
/// <summary> /// Change image size mode, refresh UI elements and resize the picture box /// </summary> /// <param name="sz">New image size mode to be set</param> private void setImageSizeMode(ImageSizeMode sz) { if (sz == ImageSizeMode.Restore) { sz = Settings.Get.Display.SizeMode; } Settings.Get.Display.SizeMode = sz; refreshImageSizeModeUI(sz); pictureBox.SizeMode = (ImageView.SizeMode)sz; }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImage">源图</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void SaveThumbnail(Image originalImage, string thumbnailPath, ImageSizeMode mode, int width, int height) { //生成缩略图 MakeThumbnail(originalImage, mode, width, height, (img) => { //以jpg格式保存缩略图 img.Save(thumbnailPath, ImageFormat.Jpeg); }).Dispose(); //释放资源 originalImage.Dispose(); }
public void Load(XmlDocument doc) { XmlNode n; ImageSizeMode sz; n = doc.SelectSingleNode("/Settings/Reader/SizeMode"); if (n != null && Enum.TryParse(n.InnerText, out sz)) { SizeMode = sz; } else { SizeMode = DEFAULT_IMAGESIZE_MODE; } }
/// <summary> /// Handles ImageEditSizeMode changes /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CbxSizeModeSelectedIndexChanged(object sender, EventArgs e) { if (cbxSizeMode.SelectedIndex >= 0) { GridPanel panel = superGridControl1.PrimaryGrid; GridColumn column = panel.Columns["ImageEdit"]; ImageSizeMode sizeMode = (ImageSizeMode)Enum.Parse( typeof(ImageSizeMode), (string)cbxSizeMode.SelectedItem); SetNewSizeMode(column.EditControl as GridImageEditControl, sizeMode); SetNewSizeMode(column.RenderControl as GridImageEditControl, sizeMode); column.InvalidateRender(); } }
public static void DrawImage(Graphics g, Image image, Rectangle destRect, ImageSizeMode sizeMode, Padding margins) { switch (sizeMode) { case ImageSizeMode.Centered: DrawImageUnscaled(g, image, destRect.X, destRect.Y); break; case ImageSizeMode.Stretched: if (margins == Padding.Empty) g.DrawImage(image, destRect); else DrawImage(g, image, destRect, null, margins); break; case ImageSizeMode.Tiled: DrawImageTiled(g, image, destRect); break; } }
private void refreshImageSizeModeUI(ImageSizeMode sizeMode) { switch (sizeMode) { case ImageSizeMode.BestFit: BestFitToolStripMenuItem.Image = Properties.Resources.expand_arrows_tick16; realSizeToolStripMenuItem.Image = ImageView.Properties.Resources.expand_solid16; fitToWidthToolStripMenuItem.Image = ImageView.Properties.Resources.fith16; fitToHeightToolStripMenuItem.Image = ImageView.Properties.Resources.fitv16; break; case ImageSizeMode.RealSize: BestFitToolStripMenuItem.Image = ImageView.Properties.Resources.expand_arrows16; realSizeToolStripMenuItem.Image = ImageView.Properties.Resources.expand_solid_tick16; fitToWidthToolStripMenuItem.Image = ImageView.Properties.Resources.fith16; fitToHeightToolStripMenuItem.Image = ImageView.Properties.Resources.fitv16; break; case ImageSizeMode.FitToWidth: BestFitToolStripMenuItem.Image = ImageView.Properties.Resources.expand_arrows16; realSizeToolStripMenuItem.Image = ImageView.Properties.Resources.expand_solid16; fitToWidthToolStripMenuItem.Image = ImageView.Properties.Resources.fith_tick16; fitToHeightToolStripMenuItem.Image = ImageView.Properties.Resources.fitv16; break; case ImageSizeMode.FitToHeight: BestFitToolStripMenuItem.Image = ImageView.Properties.Resources.expand_arrows16; realSizeToolStripMenuItem.Image = ImageView.Properties.Resources.expand_solid16; fitToWidthToolStripMenuItem.Image = ImageView.Properties.Resources.fith16; fitToHeightToolStripMenuItem.Image = ImageView.Properties.Resources.fitv_tick16; break; default: BestFitToolStripMenuItem.Image = ImageView.Properties.Resources.expand_arrows16; realSizeToolStripMenuItem.Image = ImageView.Properties.Resources.expand_solid16; fitToWidthToolStripMenuItem.Image = ImageView.Properties.Resources.fith16; fitToHeightToolStripMenuItem.Image = ImageView.Properties.Resources.fitv16; break; } }
public static void DrawImage(Graphics g, Image image, Rectangle destRect, ImageSizeMode sizeMode, Padding margins) { switch (sizeMode) { case ImageSizeMode.Centered: DrawImageUnscaled(g, image, destRect.X, destRect.Y); break; case ImageSizeMode.Stretched: if (margins == Padding.Empty) { g.DrawImage(image, destRect); } else { DrawImage(g, image, destRect, null, margins); } break; case ImageSizeMode.Tiled: DrawImageTiled(g, image, destRect); break; } }
/// <summary> /// /// </summary> /// <param name="size"></param> /// <param name="resize"></param> /// <param name="mode"></param> /// <returns></returns> public static Size GetSize(Size size, Size resize, ImageSizeMode mode) { // int toWidth = resize.Width; // int toHeight = resize.Height; // int x = 0; // int y = 0; // int ow = size.Width; // int oh = size.Height; Size newSize = new Size(); newSize.Width = resize.Width; newSize.Height = resize.Height; switch (mode) { case ImageSizeMode.SuitWidth: newSize.Height = size.Height * resize.Width / size.Width; break; case ImageSizeMode.SuitHeight: newSize.Width = size.Width * resize.Height / size.Height; break; case ImageSizeMode.AutoSuit: //根据宽度适配 if (size.Width > size.Height) { newSize.Height = size.Height * resize.Width / size.Width; } else //根据高度适配 { newSize.Width = size.Width * resize.Height / size.Height; } break; //填充适应 case ImageSizeMode.FillFit: if ((double)size.Width / (double)size.Height > (double)resize.Width / (double)resize.Height) { newSize.Height = size.Height * resize.Width / size.Width; } else { newSize.Width = resize.Width * resize.Height / size.Height; } break; //裁剪 case ImageSizeMode.Cut: //裁剪宽 if ((double)size.Width / (double)size.Height > (double)resize.Width / (double)resize.Height) { newSize.Height = size.Height; newSize.Width = size.Height * resize.Width / resize.Height; } else { newSize.Width = size.Width; newSize.Height = size.Width * resize.Height / resize.Width; } break; default: case ImageSizeMode.CustomSize: break; } return(newSize); }
public ConfigReader() { SizeMode = DEFAULT_IMAGESIZE_MODE; }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="thumbHandler">对缩略图处理</param> public static MemoryStream MakeThumbnail(Image original, ImageSizeMode mode, int width, int height, ImageGraphicsHandler thumbHandler) { int toWidth = width; int toHeight = height; int x = 0; int y = 0; int ow = original.Width; int oh = original.Height; switch (mode) { case ImageSizeMode.SuitWidth: toHeight = original.Height * width / original.Width; break; case ImageSizeMode.SuitHeight: toWidth = original.Width * height / original.Height; break; case ImageSizeMode.AutoSuit: //根据宽度适配 if (original.Width > original.Height) { toHeight = original.Height * width / original.Width; } else //根据高度适配 { toWidth = original.Width * height / original.Height; } break; //填充适应 case ImageSizeMode.FillFit: if ((double)original.Width / (double)original.Height > (double)toWidth / (double)toHeight) { toWidth = width; toHeight = original.Height * width / original.Width; } else { toHeight = height; toWidth = original.Width * height / original.Height; } break; //裁剪 case ImageSizeMode.Cut: if ((double)original.Width / (double)original.Height > (double)toWidth / (double)toHeight) { oh = original.Height; ow = original.Height * toWidth / toHeight; y = 0; x = (original.Width - ow) / 2; } else { ow = original.Width; oh = original.Width * height / toWidth; x = 0; y = (original.Height - oh) / 2; } break; default: case ImageSizeMode.CustomSize: break; } //新建一个bmp图片 Image bitmap; if (mode == ImageSizeMode.FillFit) { bitmap = new Bitmap(width, height); } else { bitmap = new Bitmap(toWidth, toHeight); } //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); if (mode == ImageSizeMode.FillFit) { g.Clear(Color.White); } //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(original, new Rectangle((width - toWidth) / 2, (height - toHeight) / 2, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); //对图片进行处理 if (thumbHandler != null) { thumbHandler(bitmap); if (bitmap == null) { throw new ArgumentException("不允许释放图片资源!"); } } //保存到内存留 MemoryStream ms = new MemoryStream(); //图片质量参数 EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(Encoder.Quality, 100L); ep.Param[1] = new EncoderParameter(Encoder.Compression, 60L); ImageCodecInfo imgCode = null; ImageCodecInfo[] imgCodes = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo code in imgCodes) { if (code.MimeType == "image/jpeg") { imgCode = code; break; } } if (imgCode != null) { bitmap.Save(ms, imgCode, ep); } else { bitmap.Save(ms, ImageFormat.Jpeg); } //释放资源 g.Dispose(); bitmap.Dispose(); return(ms); }
void DrawImgFld(Image img, int x, int y, ImageSizeMode sizeMode, int width, int height, MprStlRect stlBox) { int tX = x, tY = y, tWidth = width, tHeight = height; if (img != null) { switch (sizeMode) { case ImageSizeMode.RealSize: tWidth = img.Width; tHeight = img.Height; break; case ImageSizeMode.Zoom: float aR = (float)img.Width / (float)img.Height; tWidth = width; tHeight = (int)(tWidth / aR); if (tHeight <= height) { tY += (height - tHeight) / 2; } else { tHeight = height; tWidth = (int)(tHeight * aR); tX += (width - tWidth) / 2; } break; default: break; } DrawImg(img, tX, tY, tWidth, tHeight); DrawRect(x, y, width, height, stlBox); } }
public void Load(XmlDocument doc) { XmlNode n; bool bvalue; //int ivalue; float fvalue; //Auto rotate n = doc.SelectSingleNode("/Settings/Display/AutoRotate"); if (n != null && bool.TryParse(n.InnerText, out bvalue)) { AutoRotate = bvalue; } else { AutoRotate = DEFAULT_AUTO_ROTATE; } //CheckeredPatternBackground n = doc.SelectSingleNode("/Settings/Display/CheckeredPatternBackground"); if (n != null && bool.TryParse(n.InnerText, out bvalue)) { CheckeredPatternBackground = bvalue; } else { CheckeredPatternBackground = DEFAULT_CHECKERED_PATTERN_BACKGROUND; } //zoom n = doc.SelectSingleNode("/Settings/Display/Zoom"); if (n != null && float.TryParse(n.InnerText, out fvalue) && fvalue > 0.0f) { Zoom = fvalue; } else { Zoom = DEFAULT_ZOOM; } //zoom step n = doc.SelectSingleNode("/Settings/Display/ZoomStep"); if (n != null && float.TryParse(n.InnerText, out fvalue) && fvalue > 0.0f) { ZoomStep = fvalue; } else { ZoomStep = DEFAULT_ZOOM_STEP; } //image size mode ImageSizeMode sz; n = doc.SelectSingleNode("/Settings/Display/SizeMode"); if (n != null && Enum.TryParse(n.InnerText, out sz)) { SizeMode = sz; } else { SizeMode = DEFAULT_IMAGESIZEMODE; } //SizeModeOnNextImage n = doc.SelectSingleNode("/Settings/Display/SizeModeOnImageLoad"); if (n != null && Enum.TryParse(n.InnerText, out sz)) { SizeModeOnImageLoad = sz; } else { SizeModeOnImageLoad = DEFAULT_IMAGESIZEMODE; } }
public ConfigSlideshow() { Timer = DEFAULT_SLIDESHOW_TIMER; SizeMode = ConfigSlideshow.DEFAULT_IMAGESIZE_MODE; }
/// <summary> /// 重新绘制指定尺寸的图片 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="format">图像格式</param> /// <param name="graphicsHandler">对缩略图处理</param> public static byte[] DrawBySize( Image original, ImageSizeMode mode, int width, int height, ImageFormat format, long imageQuality, long compression, ImageGraphicsHandler graphicsHandler) { int toWidth = width; int toHeight = height; int x = 0; int y = 0; int ow = original.Width; //原始宽度 int oh = original.Height; //原始高度 Size toSize = new Size(width, height); //要转换的图片尺寸 Size imageSize = GetSize(original.Size, toSize, mode); //转换的实际尺寸 //裁剪 if (mode == ImageSizeMode.Cut) { if ((double) original.Width/(double) original.Height > (double) toSize.Width/(double) toSize.Height) { oh = original.Height; ow = original.Height*toWidth/toHeight; y = 0; x = (original.Width - ow)/2; } else { ow = original.Width; oh = original.Width*height/toWidth; x = 0; y = (original.Height - oh)/2; } } else { x = (toSize.Width - imageSize.Width)/2; y = (toSize.Height - imageSize.Height)/2; } //新建一个bmp图片 Image bitmap; if (mode == ImageSizeMode.FillFit) { bitmap = new Bitmap(width, height); } else { bitmap = new Bitmap(toWidth, toHeight); } //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); if (mode == ImageSizeMode.FillFit) { g.Clear(Color.White); } //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(original, new Rectangle(x, y, imageSize.Width, imageSize.Height), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); //对图片进行处理 if (graphicsHandler != null) { graphicsHandler(bitmap); if (bitmap == null) { throw new ArgumentException("不允许释放图片资源!"); } } //保存到内存留 MemoryStream ms = new MemoryStream(); ImageCodecInfo imgCode = null; if (format != null) { string formatStr = format.ToString(); imgCode = imageCoders.ContainsKey(formatStr) ? imageCoders[formatStr] : null; } if (imgCode != null) { EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(Encoder.Quality, imageQuality > 100 || imageQuality <= 0 ? 100L : imageQuality); ep.Param[1] = new EncoderParameter(Encoder.Compression, compression < 1L ? 100L : compression); bitmap.Save(ms, imgCode, ep); } else { bitmap.Save(ms, ImageFormat.Jpeg); } //释放资源 g.Dispose(); bitmap.Dispose(); return ms.ToArray(); }
public MyGridImageEditControl(ImageList imageList, ImageSizeMode sizeMode) { ImageList = imageList; ImageSizeMode = sizeMode; }
public static Size GetSize(Size size, Size resize, ImageSizeMode mode) { // int toWidth = resize.Width; // int toHeight = resize.Height; // int x = 0; // int y = 0; // int ow = size.Width; // int oh = size.Height; Size newSize = new Size(); newSize.Width = resize.Width; newSize.Height = resize.Height; switch (mode) { case ImageSizeMode.SuitWidth: newSize.Height = size.Height*resize.Width/size.Width; break; case ImageSizeMode.SuitHeight: newSize.Width = size.Width*resize.Height/size.Height; break; case ImageSizeMode.AutoSuit: //根据宽度适配 if (size.Width > size.Height) { newSize.Height = size.Height*resize.Width/size.Width; } else //根据高度适配 { newSize.Width = size.Width*resize.Height/size.Height; } break; //填充适应 case ImageSizeMode.FillFit: if ((double) size.Width/(double) size.Height > (double) resize.Width/(double) resize.Height) { newSize.Height = size.Height*resize.Width/size.Width; } else { newSize.Width = resize.Width*resize.Height/size.Height; } break; //裁剪 case ImageSizeMode.Cut: //裁剪宽 if ((double) size.Width/(double) size.Height > (double) resize.Width/(double) resize.Height) { newSize.Height = size.Height; newSize.Width = size.Height*resize.Width/resize.Height; } else { newSize.Width = size.Width; newSize.Height = size.Width*resize.Height/resize.Width; } break; default: case ImageSizeMode.CustomSize: break; } return newSize; }
/// <summary> /// 重新绘制指定尺寸的图片 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="graphicsHandler">对缩略图处理</param> public static byte[] DrawBySize( Image original, ImageSizeMode mode, int width, int height, ImageGraphicsHandler graphicsHandler) { return DrawBySize(original, mode, width, height, ImageFormat.Jpeg, 100L, 60L, graphicsHandler); }
/// <summary> /// 图片自动大小扩展,支持网络图片 /// </summary> /// <param name="url"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="source"></param> /// <param name="mode"></param> /// <returns></returns> public static string ImageAutoSize(this UrlHelper url, int width, int height, string source, ImageSizeMode mode, ref int _thumHeight) { //return url.Action("ImageAutoSize", "ImageAutoSize", new { w = width, h = height, s = source, i = (int)mode }); return(new ImageAutoSizeController().ImageAutoSize(source, width, height, (Int16?)mode, ref _thumHeight).Content.ToString2()); }
/// <summary> /// Initializes a new instance of the CellImageStyle class with default settings /// </summary> public CellImageStyle() { this.image = null; this.imageSizeMode = ImageSizeMode.Normal; }
public Picturebox(BaseRenderer renderer) { Renderer = renderer; SizeMode = ImageSizeMode.Zoom; }
/// <summary> /// Gets the Rectangle that specifies the Size and Location of /// the Image contained in the current Cell /// </summary> /// <param name="image">The Image to be drawn</param> /// <param name="sizeMode">An ImageSizeMode that specifies how the /// specified Image is scaled</param> /// <param name="rowAlignment">The alignment of the current Cell's row</param> /// <param name="columnAlignment">The alignment of the current Cell's Column</param> /// <returns>A Rectangle that specifies the Size and Location of /// the Image contained in the current Cell</returns> protected Rectangle CalcImageRect(Image image, ImageSizeMode sizeMode, RowAlignment rowAlignment, ColumnAlignment columnAlignment) { if (this.DrawText) { sizeMode = ImageSizeMode.ScaledToFit; } Rectangle imageRect = this.ClientRectangle; if (sizeMode == ImageSizeMode.Normal) { if (image.Width < imageRect.Width) { imageRect.Width = image.Width; } if (image.Height < imageRect.Height) { imageRect.Height = image.Height; } } else if (sizeMode == ImageSizeMode.ScaledToFit) { if (image.Width >= imageRect.Width || image.Height >= imageRect.Height) { double hScale = ((double) imageRect.Width) / ((double) image.Width); double vScale = ((double) imageRect.Height) / ((double) image.Height); double scale = Math.Min(hScale, vScale); imageRect.Width = (int) (((double) image.Width) * scale); imageRect.Height = (int) (((double) image.Height) * scale); } else { imageRect.Width = image.Width; imageRect.Height = image.Height; } } if (rowAlignment == RowAlignment.Center) { imageRect.Y += (this.ClientRectangle.Height - imageRect.Height) / 2; } else if (rowAlignment == RowAlignment.Bottom) { imageRect.Y = this.ClientRectangle.Bottom - imageRect.Height; } if (!this.DrawText) { if (columnAlignment == ColumnAlignment.Center) { imageRect.X += (this.ClientRectangle.Width - imageRect.Width) / 2; } else if (columnAlignment == ColumnAlignment.Right) { imageRect.X = this.ClientRectangle.Width - imageRect.Width; } } return imageRect; }
public static void DrawImage(this Graphics graphics, System.Drawing.Image image, Rectangle bounds, ImageSizeMode sizeMode) { int x = bounds.X; int y = bounds.Y; int w = image.Width; int h = image.Height; switch (sizeMode) { case ImageSizeMode.Stretch: w = bounds.Width; h = bounds.Height; break; case ImageSizeMode.Center: x += (int)((float)bounds.Width / 2 - (float)w / 2); y += (int)((float)bounds.Height / 2 - (float)h / 2); break; case ImageSizeMode.Zoom: float scaleFactor = Math.Min((float)bounds.Width / image.Width, (float)bounds.Height / image.Height); w = (int)(w * scaleFactor); h = (int)(h * scaleFactor); x += (int)((float)bounds.Width / 2 - (float)w / 2); y += (int)((float)bounds.Height / 2 - (float)h / 2); break; } graphics.DrawImage(image, x, y, w, h); }
/// <summary> /// 重新绘制指定尺寸的图片 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="format">图像格式</param> /// <param name="compression"></param> /// <param name="graphicsHandler">对缩略图处理</param> /// <param name="imageQuality"></param> public static byte[] DrawBySize( Image original, ImageSizeMode mode, int width, int height, ImageFormat format, long imageQuality, long compression, ImageGraphicsHandler graphicsHandler) { int toWidth = width; int toHeight = height; int x = 0; int y = 0; int ow = original.Width; //原始宽度 int oh = original.Height; //原始高度 Size toSize = new Size(width, height); //要转换的图片尺寸 Size imageSize = GetSize(original.Size, toSize, mode); //转换的实际尺寸 //裁剪 if (mode == ImageSizeMode.Cut) { if ((double)original.Width / (double)original.Height > (double)toSize.Width / (double)toSize.Height) { oh = original.Height; ow = original.Height * toWidth / toHeight; y = 0; x = (original.Width - ow) / 2; } else { ow = original.Width; oh = original.Width * height / toWidth; x = 0; y = (original.Height - oh) / 2; } } else { x = (toSize.Width - imageSize.Width) / 2; y = (toSize.Height - imageSize.Height) / 2; } //新建一个bmp图片 Image bitmap; if (mode == ImageSizeMode.FillFit) { bitmap = new Bitmap(width, height); } else { bitmap = new Bitmap(toWidth, toHeight); } //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); if (mode == ImageSizeMode.FillFit) { g.Clear(Color.White); } //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(original, new Rectangle(x, y, imageSize.Width, imageSize.Height), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); //对图片进行处理 if (graphicsHandler != null) { graphicsHandler(bitmap); if (bitmap == null) { throw new ArgumentException("不允许释放图片资源!"); } } //保存到内存留 MemoryStream ms = new MemoryStream(); ImageCodecInfo imgCode = null; if (format != null) { string formatStr = format.ToString(); imgCode = imageCoders.ContainsKey(formatStr) ? imageCoders[formatStr] : null; } if (imgCode != null) { EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(Encoder.Quality, imageQuality > 100 || imageQuality <= 0 ? 100L : imageQuality); ep.Param[1] = new EncoderParameter(Encoder.Compression, compression < 1L ? 100L : compression); bitmap.Save(ms, imgCode, ep); } else { bitmap.Save(ms, ImageFormat.Jpeg); } //释放资源 g.Dispose(); byte[] bytes = ms.ToArray(); ms.Dispose(); bitmap.Dispose(); return(bytes); }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="original">原图</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">缩略图模式</param> /// <param name="thumbHandler">对缩略图处理</param> public static MemoryStream MakeThumbnail(Image original, ImageSizeMode mode, int width, int height, ImageGraphicsHandler thumbHandler) { int toWidth = width; int toHeight = height; int x = 0; int y = 0; int ow = original.Width; int oh = original.Height; switch (mode) { case ImageSizeMode.SuitWidth: toHeight = original.Height*width/original.Width; break; case ImageSizeMode.SuitHeight: toWidth = original.Width*height/original.Height; break; case ImageSizeMode.AutoSuit: //根据宽度适配 if (original.Width > original.Height) { toHeight = original.Height*width/original.Width; } else //根据高度适配 { toWidth = original.Width*height/original.Height; } break; //填充适应 case ImageSizeMode.FillFit: if ((double) original.Width/(double) original.Height > (double) toWidth/(double) toHeight) { toWidth = width; toHeight = original.Height*width/original.Width; } else { toHeight = height; toWidth = original.Width*height/original.Height; } break; //裁剪 case ImageSizeMode.Cut: if ((double) original.Width/(double) original.Height > (double) toWidth/(double) toHeight) { oh = original.Height; ow = original.Height*toWidth/toHeight; y = 0; x = (original.Width - ow)/2; } else { ow = original.Width; oh = original.Width*height/toWidth; x = 0; y = (original.Height - oh)/2; } break; default: case ImageSizeMode.CustomSize: break; } //新建一个bmp图片 Image bitmap; if (mode == ImageSizeMode.FillFit) { bitmap = new Bitmap(width, height); } else { bitmap = new Bitmap(toWidth, toHeight); } //新建一个画板 Graphics g = Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); if (mode == ImageSizeMode.FillFit) { g.Clear(Color.White); } //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(original, new Rectangle((width - toWidth)/2, (height - toHeight)/2, toWidth, toHeight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); //对图片进行处理 if (thumbHandler != null) { thumbHandler(bitmap); if (bitmap == null) { throw new ArgumentException("不允许释放图片资源!"); } } //保存到内存留 MemoryStream ms = new MemoryStream(); //图片质量参数 EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 60L); ImageCodecInfo imgCode = null; ImageCodecInfo[] imgCodes = ImageCodecInfo.GetImageEncoders(); foreach (ImageCodecInfo code in imgCodes) { if (code.MimeType == "image/jpeg") { imgCode = code; break; } } if (imgCode != null) { bitmap.Save(ms, imgCode, ep); } else { bitmap.Save(ms, ImageFormat.Jpeg); } //释放资源 g.Dispose(); bitmap.Dispose(); return ms; }
/// <summary> /// Gets the Rectangle that specifies the Size and Location of /// the Image contained in the current Cell /// </summary> /// <param name="image">The Image to be drawn</param> /// <param name="sizeMode">An ImageSizeMode that specifies how the /// specified Image is scaled</param> /// <param name="rowAlignment">The alignment of the current Cell's row</param> /// <param name="columnAlignment">The alignment of the current Cell's Column</param> /// <returns>A Rectangle that specifies the Size and Location of /// the Image contained in the current Cell</returns> protected Rectangle CalcImageRect(Image image, ImageSizeMode sizeMode, RowAlignment rowAlignment, ColumnAlignment columnAlignment) { if (this.DrawText) { sizeMode = ImageSizeMode.ScaledToFit; } Rectangle imageRect = this.ClientRectangle; if (sizeMode == ImageSizeMode.Normal) { if (image.Width < imageRect.Width) { imageRect.Width = image.Width; } if (image.Height < imageRect.Height) { imageRect.Height = image.Height; } } else if (sizeMode == ImageSizeMode.ScaledToFit) { if (image.Width >= imageRect.Width || image.Height >= imageRect.Height) { double hScale = ((double)imageRect.Width) / ((double)image.Width); double vScale = ((double)imageRect.Height) / ((double)image.Height); double scale = Math.Min(hScale, vScale); imageRect.Width = (int)(((double)image.Width) * scale); imageRect.Height = (int)(((double)image.Height) * scale); } else { imageRect.Width = image.Width; imageRect.Height = image.Height; } } else if (sizeMode == ImageSizeMode.NoClip) { imageRect.Width = image.Width; imageRect.Height = image.Height; } if (rowAlignment == RowAlignment.Center) { imageRect.Y += (this.ClientRectangle.Height - imageRect.Height) / 2; } else if (rowAlignment == RowAlignment.Bottom) { imageRect.Y = this.ClientRectangle.Bottom - imageRect.Height; } if (!this.DrawText) { if (columnAlignment == ColumnAlignment.Center) { imageRect.X += (this.ClientRectangle.Width - imageRect.Width) / 2; } else if (columnAlignment == ColumnAlignment.Right) { imageRect.X = this.ClientRectangle.Width - imageRect.Width; } } return(imageRect); }