在c#中调整图像大小而不丢失右下像素部分

发布于 2024-08-15 09:59:23 字数 1349 浏览 4 评论 0原文

我的 ac#-class 看起来大致如下:

 class ImageContainer
 {
  Image image;
  internal ImageContainer getResized(int width, int height)
  {
   Bitmap bmp = new Bitmap(width, height);
   //Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
   System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
   //Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
   gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   //Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
   gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
   //Set the System.Drawing.Graphics object property InterpolationMode to High
   gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
   //Draw the original image into the target Graphics object scaling to the desired width and height
   System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, width, height);
   gr.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
   //dispose / release  resources

   ImageContainer ic = new ImageContainer();
   ic.image = bmp;

   return ic;
  }
 }

调整大小工作正常,但在缩小图像时,DrawImage 不会绘制最右侧的较低像素片段。

I have a c#-class that looks roughly like this:

 class ImageContainer
 {
  Image image;
  internal ImageContainer getResized(int width, int height)
  {
   Bitmap bmp = new Bitmap(width, height);
   //Create a System.Drawing.Graphics object from the Bitmap which we will use to draw the high quality scaled image
   System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
   //Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
   gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   //Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
   gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
   //Set the System.Drawing.Graphics object property InterpolationMode to High
   gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
   //Draw the original image into the target Graphics object scaling to the desired width and height
   System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, width, height);
   gr.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
   //dispose / release  resources

   ImageContainer ic = new ImageContainer();
   ic.image = bmp;

   return ic;
  }
 }

The resizing works fine, but DrawImage doesn't draw the most right an lower pixel-fragments when scaling down an image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性 2024-08-22 09:59:23

问题解决了

gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

:)

the problem is solved by

gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文