| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778 |
-
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Runtime.InteropServices;
- namespace Dongke.WinForm.Controls.InvoiceLayout.DrawBarcode
- {
- /// <summary>
- /// 图片操作类
- /// </summary>
- public static class ImageHelper
- {
- #region 平铺
- // 水平 左 中 右
- // 垂直 上 中 下
- /// <summary>
- /// 平铺
- /// </summary>
- /// <param name="image"></param>
- /// <param name="size"></param>
- /// <param name="alignment"></param>
- /// <returns></returns>
- public static Image Tile(Image image, Size size, ContentAlignment alignment = ContentAlignment.TopLeft)
- {
- Image targetImage = new Bitmap(size.Width, size.Height);
- Tile(image, targetImage, null, alignment);
- return targetImage;
- }
- /// <summary>
- /// 平铺
- /// </summary>
- /// <param name="image"></param>
- /// <param name="targetImage"></param>
- /// <param name="rectangle"></param>
- /// <param name="alignment"></param>
- public static void Tile(Image image, Image targetImage, Rectangle? rectangle = null,
- ContentAlignment alignment = ContentAlignment.TopLeft)
- {
- if (rectangle == null)
- {
- rectangle = new Rectangle(0, 0, targetImage.Width, targetImage.Height);
- }
- else
- {
- rectangle = Rectangle.Intersect(rectangle.Value,
- new Rectangle(0, 0, targetImage.Width, targetImage.Height));
- if (rectangle.Value.IsEmpty)
- {
- return;
- }
- }
- using (Graphics graphics = Graphics.FromImage(targetImage))
- {
- Tile(image, graphics, rectangle.Value, alignment);
- }
- }
- /// <summary>
- /// 平铺
- /// </summary>
- /// <param name="image"></param>
- /// <param name="graphics"></param>
- /// <param name="rectangle"></param>
- /// <param name="alignment"></param>
- public static void Tile(Image image, Graphics graphics, Rectangle rectangle,
- ContentAlignment alignment = ContentAlignment.TopLeft)
- {
- using (TextureBrush brush = new TextureBrush(image))
- {
- SetGraphicsHighQuality(graphics);
- brush.WrapMode = WrapMode.Tile;
- //if (alignment == ContentAlignment.TopLeft)
- //{
- // graphics.FillRectangle(brush, rectangle);
- //}
- //else
- {
- int x = 0;
- int y = 0;
- switch (alignment)
- {
- case ContentAlignment.TopCenter:
- x = GetCenter(image.Width, rectangle.Width);
- break;
- case ContentAlignment.TopRight:
- x = (rectangle.Width % image.Width) - image.Width;
- break;
- case ContentAlignment.MiddleLeft:
- y = GetCenter(image.Height, rectangle.Height);
- break;
- case ContentAlignment.MiddleCenter:
- x = GetCenter(image.Width, rectangle.Width);
- y = GetCenter(image.Height, rectangle.Height);
- break;
- case ContentAlignment.MiddleRight:
- x = (rectangle.Width % image.Width) - image.Width;
- y = GetCenter(image.Height, rectangle.Height);
- break;
- case ContentAlignment.BottomLeft:
- y = (rectangle.Height % image.Height) - image.Height;
- break;
- case ContentAlignment.BottomCenter:
- x = GetCenter(image.Width, rectangle.Width);
- y = (rectangle.Height % image.Height) - image.Height;
- break;
- case ContentAlignment.BottomRight:
- x = (rectangle.Width % image.Width) - image.Width;
- y = (rectangle.Height % image.Height) - image.Height;
- break;
- default:
- break;
- }
- Rectangle rect = new Rectangle(0, 0,
- (x < 0 ? rectangle.Width - x : rectangle.Width),
- (y < 0 ? rectangle.Height - y : rectangle.Height));
- using (Bitmap tmp = new Bitmap(rect.Width, rect.Height))
- {
- using (Graphics tmpg = Graphics.FromImage(tmp))
- {
- SetGraphicsHighQuality(tmpg);
- tmpg.FillRectangle(brush, rect);
- Rectangle rectImage = new Rectangle(Math.Abs(x), Math.Abs(y),
- rectangle.Width,
- rectangle.Height);
- graphics.DrawImage(tmp, rectangle.X, rectangle.Y, rectImage, GraphicsUnit.Pixel);
- }
- }
- }
- }
- }
- private static int GetCenter(int size1, int size2)
- {
- int t = (size2 - size1);
- if (t > 0)
- {
- return ((t / 2) % size1) - size1;
- }
- else if (t < 0)
- {
- return (t / 2);
- }
- return 0;
- }
- #endregion
- #region 拉伸
- /// <summary>
- /// 拉伸
- /// </summary>
- /// <param name="image"></param>
- /// <param name="size"></param>
- /// <returns></returns>
- public static Image Stretch(Image image, Size size)
- {
- //Image targetImage = new Bitmap(image, size);
- Image targetImage = new Bitmap(size.Width, size.Height);
- Stretch(image, targetImage);
- return targetImage;
- }
- /// <summary>
- /// 拉伸
- /// </summary>
- /// <param name="image"></param>
- /// <param name="targetImage"></param>
- /// <param name="rectangle"></param>
- public static void Stretch(Image image, Image targetImage, Rectangle? rectangle = null)
- {
- if (rectangle == null)
- {
- rectangle = new Rectangle(0, 0, targetImage.Width, targetImage.Height);
- }
- else
- {
- rectangle = Rectangle.Intersect(rectangle.Value,
- new Rectangle(0, 0, targetImage.Width, targetImage.Height));
- if (rectangle.Value.IsEmpty)
- {
- return;
- }
- }
- using (Graphics graphics = Graphics.FromImage(targetImage))
- {
- Stretch(image, graphics, rectangle.Value);
- }
- }
- /// <summary>
- /// 拉伸
- /// </summary>
- /// <param name="image"></param>
- /// <param name="graphics"></param>
- /// <param name="rectangle"></param>
- public static void Stretch(Image image, Graphics graphics, Rectangle rectangle)
- {
- SetGraphicsHighQuality(graphics);
- graphics.DrawImage(image, rectangle);
- }
- #endregion
- #region 缩放(按比例)
- /// <summary>
- /// 缩放(按比例)类型
- /// </summary>
- public enum ZoomType
- {
- /// <summary>
- /// 自适应
- /// </summary>
- Adaptive = 1,
- /// <summary>
- /// 比例填充(超出部分居中)
- /// </summary>
- Filled,
- /// <summary>
- /// 按宽显示(超出部分居中)
- /// </summary>
- WidthRatio,
- /// <summary>
- /// 按高显示(超出部分居中)
- /// </summary>
- HeightRatio,
- }
- private static void ZoomWidth(Image image, Graphics graphics, Rectangle rectangle)
- {
- decimal ratio = rectangle.Width * 1m / image.Width;
- int height = (int)(image.Height * ratio);
- Rectangle rect = new Rectangle(0, 0, rectangle.Width, height);
- //using (Image tmp = Stretch(image, rect.Size))
- {
- SetGraphicsHighQuality(graphics);
- if (height > rectangle.Height)
- {
- rect.Width = image.Width;
- rect.Height = (int)(rectangle.Height / ratio);
- rect.Y = (image.Height - rect.Height) / 2;
- graphics.DrawImage(image, rectangle, rect, GraphicsUnit.Pixel);
- }
- else
- {
- rect.X = rectangle.X;
- rect.Y = rectangle.Y + (rectangle.Height - height) / 2;
- graphics.DrawImage(image, rect);
- }
- }
- }
- private static void ZoomHeight(Image image, Graphics graphics, Rectangle rectangle)
- {
- decimal ratio = rectangle.Height * 1m / image.Height;
- int width = (int)(image.Width * ratio);
- Rectangle rect = new Rectangle(0, 0, width, rectangle.Height);
- //using (Image tmp = Stretch(image, rect.Size))
- {
- SetGraphicsHighQuality(graphics);
- if (width > rectangle.Width)
- {
- rect.Height = image.Height;
- rect.Width = (int)(rectangle.Width / ratio);
- rect.X = (image.Width - rect.Width) / 2;
- graphics.DrawImage(image, rectangle, rect, GraphicsUnit.Pixel);
- }
- else
- {
- rect.X = rectangle.X + (rectangle.Width - width) / 2;
- rect.Y = rectangle.Y;
- graphics.DrawImage(image, rect);
- }
- }
- }
- /// <summary>
- /// 缩放(按比例)
- /// </summary>
- /// <param name="image"></param>
- /// <param name="size"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public static Image Zoom(Image image, Size size, ZoomType type = ZoomType.Adaptive)
- {
- //Image targetImage = new Bitmap(image, size);
- Image targetImage = new Bitmap(size.Width, size.Height);
- Zoom(image, targetImage, null, type);
- return targetImage;
- }
- /// <summary>
- /// 缩放(按比例)
- /// </summary>
- /// <param name="image"></param>
- /// <param name="targetImage"></param>
- /// <param name="rectangle"></param>
- /// <param name="type"></param>
- public static void Zoom(Image image, Image targetImage, Rectangle? rectangle = null,
- ZoomType type = ZoomType.Adaptive)
- {
- if (rectangle == null)
- {
- rectangle = new Rectangle(0, 0, targetImage.Width, targetImage.Height);
- }
- else
- {
- rectangle = Rectangle.Intersect(rectangle.Value,
- new Rectangle(0, 0, targetImage.Width, targetImage.Height));
- if (rectangle.Value.IsEmpty)
- {
- return;
- }
- }
- using (Graphics graphics = Graphics.FromImage(targetImage))
- {
- Zoom(image, graphics, rectangle.Value, type);
- }
- }
- /// <summary>
- /// 缩放(按比例)
- /// </summary>
- /// <param name="image"></param>
- /// <param name="graphics"></param>
- /// <param name="rectangle"></param>
- /// <param name="type"></param>
- public static void Zoom(Image image, Graphics graphics, Rectangle rectangle,
- ZoomType type = ZoomType.Adaptive)
- {
- switch (type)
- {
- case ZoomType.WidthRatio:
- ZoomWidth(image, graphics, rectangle);
- break;
- case ZoomType.HeightRatio:
- ZoomHeight(image, graphics, rectangle);
- break;
- default:
- decimal ratioHeight = rectangle.Height * 1m / image.Height;
- decimal ratioWidth = rectangle.Width * 1m / image.Width;
- if (ratioHeight != ratioWidth)
- {
- bool isWidth = ((ratioWidth > ratioHeight) == (type == ZoomType.Filled));
- if (isWidth)
- {
- ZoomWidth(image, graphics, rectangle);
- }
- else
- {
- ZoomHeight(image, graphics, rectangle);
- }
- }
- else
- {
- graphics.DrawImage(image, rectangle);
- }
- break;
- }
- }
- #endregion
- #region 剪裁
- /// <summary>
- /// 剪裁
- /// </summary>
- /// <param name="image"></param>
- /// <param name="rectangle"></param>
- /// <returns></returns>
- public static Image Cut(Image image, Rectangle rectangle)
- {
- Rectangle rectImage = new Rectangle(0, 0, image.Width, image.Height);
- Rectangle rect = Rectangle.Intersect(rectangle, rectImage);
- if (rect.IsEmpty)
- {
- return null;
- }
- //using (Bitmap tmp = new Bitmap(rectangle.Width, rectangle.Height))
- Bitmap tmp = new Bitmap(rectangle.Width, rectangle.Height);
- try
- {
- using (Graphics graphics = Graphics.FromImage(tmp))
- {
- SetGraphicsHighQuality(graphics);
- graphics.DrawImage(image,
- (rectangle.X < 0 ? -rectangle.X : 0),
- (rectangle.Y < 0 ? -rectangle.Y : 0),
- rect, GraphicsUnit.Pixel);
- }
- return tmp;
- }
- catch
- {
- tmp.Dispose();
- throw;
- }
- }
- #endregion
- #region 显示
- // 水平 左 中 右
- // 垂直 上 中 下
- /// <summary>
- /// 显示
- /// </summary>
- /// <param name="image"></param>
- /// <param name="size"></param>
- /// <param name="alignment"></param>
- /// <param name="cut"></param>
- /// <returns></returns>
- public static Image Show(Image image, Size size,
- ContentAlignment alignment = ContentAlignment.TopLeft, bool cut = true)
- {
- Image targetImage = new Bitmap(size.Width, size.Height);
- Show(image, targetImage, null, alignment, cut);
- return targetImage;
- }
- /// <summary>
- /// 显示
- /// </summary>
- /// <param name="image"></param>
- /// <param name="targetImage"></param>
- /// <param name="rectangle"></param>
- /// <param name="alignment"></param>
- /// <param name="cut"></param>
- public static void Show(Image image, Image targetImage, Rectangle? rectangle = null,
- ContentAlignment alignment = ContentAlignment.TopLeft, bool cut = true)
- {
- if (rectangle == null)
- {
- rectangle = new Rectangle(0, 0, targetImage.Width, targetImage.Height);
- }
- else
- {
- rectangle = Rectangle.Intersect(rectangle.Value,
- new Rectangle(0, 0, targetImage.Width, targetImage.Height));
- if (rectangle.Value.IsEmpty)
- {
- return;
- }
- }
- using (Graphics graphics = Graphics.FromImage(targetImage))
- {
- Show(image, graphics, rectangle.Value, alignment, cut);
- }
- }
- /// <summary>
- /// 显示
- /// </summary>
- /// <param name="image"></param>
- /// <param name="graphics"></param>
- /// <param name="rectangle"></param>
- /// <param name="alignment"></param>
- /// <param name="cut"></param>
- public static void Show(Image image, Graphics graphics, Rectangle rectangle,
- ContentAlignment alignment = ContentAlignment.TopLeft, bool cut = true)
- {
- int x = 0;
- int y = 0;
- switch (alignment)
- {
- case ContentAlignment.TopLeft:
- break;
- case ContentAlignment.TopCenter:
- x = (rectangle.Width - image.Width) / 2;
- break;
- case ContentAlignment.TopRight:
- x = (rectangle.Width - image.Width);
- break;
- case ContentAlignment.MiddleLeft:
- y = (rectangle.Height - image.Height) / 2;
- break;
- case ContentAlignment.MiddleCenter:
- x = (rectangle.Width - image.Width) / 2;
- y = (rectangle.Height - image.Height) / 2;
- break;
- case ContentAlignment.MiddleRight:
- x = (rectangle.Width - image.Width);
- y = (rectangle.Height - image.Height) / 2;
- break;
- case ContentAlignment.BottomLeft:
- y = (rectangle.Height - image.Height);
- break;
- case ContentAlignment.BottomCenter:
- x = (rectangle.Width - image.Width) / 2;
- y = (rectangle.Height - image.Height);
- break;
- case ContentAlignment.BottomRight:
- x = (rectangle.Width - image.Width);
- y = (rectangle.Height - image.Height);
- break;
- default:
- break;
- }
- Rectangle rectIamge = new Rectangle(0, 0, image.Width, image.Height);
- if (cut)
- {
- Rectangle rect = new Rectangle(-x, -y, rectangle.Width, rectangle.Height);
- rectIamge = Rectangle.Intersect(rectIamge, rect);
- if (x > 0)
- {
- x += rectangle.X;
- }
- else
- {
- x = rectangle.X;
- }
- if (y > 0)
- {
- y += rectangle.Y;
- }
- else
- {
- y = rectangle.Y;
- }
- }
- else
- {
- x += rectangle.X;
- y += rectangle.Y;
- }
- SetGraphicsHighQuality(graphics);
- graphics.DrawImage(image, x, y, rectIamge, GraphicsUnit.Pixel);
- }
- #endregion
- #region 压缩
- /// <summary>
- /// 压缩
- /// </summary>
- /// <param name="image"></param>
- /// <param name="quality"></param>
- /// <param name="size"></param>
- /// <returns></returns>
- public static Stream ZipImage(Image image, long quality, Size? size = null)
- {
- bool isZoom = true;
- if (size == null)
- {
- isZoom = false;
- size = image.Size;
- }
- Image bitmap = image;
- if (isZoom)
- {
- using (Bitmap tmp = new Bitmap(size.Value.Width, size.Value.Height))
- {
- Stretch(image, tmp);
- bitmap = tmp;
- }
- }
- else
- {
- }
- using (EncoderParameters myEncoderParameters = new EncoderParameters(1))
- {
- using (EncoderParameter myEncoderParameter = new EncoderParameter(Encoder.Quality, quality))
- {
- myEncoderParameters.Param[0] = myEncoderParameter;
- ImageCodecInfo codecInfo = GetEncoderInfo(image.RawFormat.ToString());
- MemoryStream ms = new MemoryStream();
- bitmap.Save(ms, codecInfo, myEncoderParameters);
- return ms;
- }
- }
- }
- /// <summary>
- /// 获取图片编码信息
- /// </summary>
- private static ImageCodecInfo GetEncoderInfo(string mimeType)
- {
- foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders())
- {
- if (encoder.MimeType == mimeType)
- {
- return encoder;
- }
- }
- return null;
- }
- #endregion
- #region 设置透明
- /// <summary>
- /// 设置透明
- /// </summary>
- /// <param name="graphics"></param>
- public static void MakeTransparent(Graphics graphics)
- {
- graphics.Clear(Color.Transparent);
- }
- /// <summary>
- /// 设置透明
- /// </summary>
- /// <param name="image"></param>
- public static void MakeTransparent(Bitmap image)
- {
- image.MakeTransparent();
- }
- /// <summary>
- /// Returns a transparent background GIF image from the specified Bitmap.
- /// </summary>
- /// <param name="bitmap">The Bitmap to make transparent.</param>
- /// <param name="color">The Color to make transparent.</param>
- /// <returns>New Bitmap containing a transparent background gif.</returns>
- public static Bitmap MakeTransparentGif(Bitmap bitmap, Color color)
- {
- byte R = color.R;
- byte G = color.G;
- byte B = color.B;
- using (MemoryStream fin = new MemoryStream())
- {
- bitmap.Save(fin, ImageFormat.Gif);
- using (MemoryStream fout = new MemoryStream((int)fin.Length))
- {
- byte[] buf = new byte[256];
- byte transparentIdx = 0;
- fin.Seek(0, SeekOrigin.Begin);
- //header
- int count = fin.Read(buf, 0, 13);
- if ((buf[0] != 71) || (buf[1] != 73) || (buf[2] != 70))
- {
- return null; //GIF
- }
- fout.Write(buf, 0, 13);
- int i = 0;
- if ((buf[10] & 0x80) > 0)
- {
- i = 1 << ((buf[10] & 7) + 1) == 256 ? 256 : 0;
- }
- for (; i != 0; i--)
- {
- fin.Read(buf, 0, 3);
- if ((buf[0] == R) && (buf[1] == G) && (buf[2] == B))
- {
- transparentIdx = (byte)(256 - i);
- }
- fout.Write(buf, 0, 3);
- }
- while (true)
- {
- fin.Read(buf, 0, 1);
- fout.Write(buf, 0, 1);
- if (buf[0] != 0x21)
- {
- break;
- }
- fin.Read(buf, 0, 1);
- fout.Write(buf, 0, 1);
- bool gcePresent = (buf[0] == 0xf9);
- while (true)
- {
- fin.Read(buf, 0, 1);
- fout.Write(buf, 0, 1);
- if (buf[0] == 0)
- {
- break;
- }
- count = buf[0];
- if (fin.Read(buf, 0, count) != count)
- {
- return null;
- }
- if (gcePresent)
- {
- if (count == 4)
- {
- buf[0] |= 0x01;
- buf[3] = transparentIdx;
- }
- }
- fout.Write(buf, 0, count);
- }
- }
- while (count > 0)
- {
- count = fin.Read(buf, 0, 1);
- fout.Write(buf, 0, 1);
- }
- fin.Close();
- fout.Flush();
- return new Bitmap(fout);
- }
- }
- }
- #endregion
- /// <summary>
- /// 按像素放大整数倍
- /// </summary>
- /// <param name="image"></param>
- /// <param name="ratioWidth"></param>
- /// <param name="ratioHeight"></param>
- /// <returns></returns>
- public static Bitmap ZoomPixel(Bitmap image, int ratioWidth, int ratioHeight)
- {
- if (image == null || ratioWidth < 1 || ratioHeight < 1)
- {
- return image;
- }
- Bitmap targetImage = new Bitmap(image.Width * ratioWidth, image.Height * ratioHeight);
- BitmapData targetImageData = null;
- try
- {
- BitmapData imageData =
- image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
- ImageLockMode.ReadOnly,
- PixelFormat.Format32bppArgb);
- targetImageData =
- targetImage.LockBits(new Rectangle(0, 0, targetImage.Width, targetImage.Height),
- ImageLockMode.WriteOnly,
- PixelFormat.Format32bppArgb);
- byte[] imagePixels = new byte[imageData.Stride * imageData.Height];
- Marshal.Copy(imageData.Scan0, imagePixels, 0, imagePixels.Length);
- byte[] targetImagePixels = new byte[targetImageData.Stride * targetImageData.Height];
- int targetImageIndex = 0;
- for (int y = 0; y < image.Height; y++)
- {
- for (int rh = 0; rh < ratioHeight; rh++)
- {
- for (int x = 0; x < image.Width; x++)
- {
- for (int rw = 0; rw < ratioWidth; rw++)
- {
- int i = y * imageData.Stride + x * 4;
- targetImagePixels[targetImageIndex++] = imagePixels[i++];
- targetImagePixels[targetImageIndex++] = imagePixels[i++];
- targetImagePixels[targetImageIndex++] = imagePixels[i++];
- targetImagePixels[targetImageIndex++] = imagePixels[i++];
- }
- }
- }
- }
- Marshal.Copy(targetImagePixels, 0, targetImageData.Scan0, targetImagePixels.Length);
- }
- finally
- {
- targetImage.UnlockBits(targetImageData);
- }
- return targetImage;
- }
- /*
- // 对于GDI+,在正常的操作,Bitmap,Graphcis,DrawImage或者DrawString ,生成图片的话,会产生很多杂点,或者是图片质量不稳定..尤其是在读取图片后,生成缩略图之后,文件会被压缩而失真..
- //主要原因是因为没有重新设置Graphics的几个属性..
- //1.Graphics.SmoothingMode属性: 例如SmoothingMode.HighQuality可以产生高质量图片,但是效率低.
- //2.Graphics.CompositingQuality 属性: 例如:CompositingQuality.HighQuality也是产生高质量图,效率低下.
- //3.Graphics.InterpolationMode 属性, 例如:InterpolationMode.HighQualityBicubic与前两个也是同样的效果.
- // 这三个属性的值都是Enum,具体的Enum参数可以查看MSDN的说明,在这里就我不多说了,
- //如果是对图片进行放大,缩小,可以调整Graphics.CompositingQuality 和Graphics.InterpolationMode 两个属性,如果是图片生成, 则可以调整Graphics.SmoothingMode属性
- //另外一个问题就是关于文字生成的..按照正常的模式生成的文字, 可以很明显的看到文字带有锯齿..解决的办法也是需要修改Graphics的一个属性: Graphics.TextRenderingHint注意一点, 修改TextRenderingHint的话, 需要引入System.Drawing.Text, 例如:Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
- // 经过对这四个属性的修改,操作大部分的图片之后,产生的结果都是比较让人满意
- */
- /// <summary>
- ///
- /// </summary>
- /// <param name="graphics"></param>
- public static void SetGraphicsHighQuality(Graphics graphics)
- {
- // 设置绘图质量
- graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
- graphics.CompositingQuality = CompositingQuality.HighQuality;
- graphics.SmoothingMode = SmoothingMode.HighQuality;
- graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
- graphics.CompositingMode = CompositingMode.SourceOver;
- //graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
- }
- }
- }
|