C# winforms:graphics.DrawImage 问题

发布于 2024-08-29 20:19:02 字数 1763 浏览 3 评论 0 原文

我对 Graphics.DrawImage 方法有一个非常奇怪的问题。

我在Panel 控件中有PictureBox 控件,其AllowScroll property = true。 该程序根据用户选择的区域将图像切割成小部分。

我加载图像 300x547 并选择区域(红色矩形):

alt 文本

程序正确剪切图像:

然后,我加载另一个图像 427x640:

alt text http://img34.imageshack.us/ img34/7950/56727000.png

然后,结果我发现图像没有正确剪切。每个 img.jpg 文件都有适当的宽度和宽度。高度但绘制的图像太小: 替代文本

这是代码片段 - 它保存用户选择的位图区域:

  Image OriginalIMG= (Image)((PictureBox)panel1.Controls["picBox"]).Image.Clone()
  Bitmap bmp = new Bitmap(selectedAreaRECT.Width, selectedAreaRECT.Height);
  Graphics g = Graphics.FromImage(bmp);

  g.DrawImage(OriginalIMG, 0,0, selectedAreaRECT, GraphicsUnit.Pixel);
  g.Save();
  g.Dispose();

  bmp.Save(AppDomain.CurrentDomain.BaseDirectory + @"\Temp\" + "img1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

如您所见,图像 A 和图像 B 中的 img1.jpg 的代码是相同的。 我试图解决这个愚蠢的问题太久了,我不知道这个问题的原因是什么。我尝试了 DrawImage 方法的不同重载,但没有成功

编辑

已解决! System.Drawing.Bitmap 的默认 DPI 值为 = 96,如果我打开 DPI != 96 的图像,则会出现上述问题。为了摆脱它,我需要使用 SetResolution 方法:

Bitmap result = new Bitmap(width, height);
result.SetResolution(OriginalIMG.HorizontalResolution, OriginalIMG.VerticalResolution);

这解决了问题:) 感谢大家的帮助! :)

I have a really strange problem with Graphics.DrawImage method.

I have the PictureBox control in the Panel control with AllowScroll property = true.
The program cuts the image on small parts basing on the area selected by the user.

I load the image 300x547 and select the area (the red rectangle):

alt text

program properly cuts the image:

alt text

then, I load another image 427x640:

alt text http://img34.imageshack.us/img34/7950/56727000.png

and then, as the result I see that the image is not cut properly. Each img.jpg file has properly width & height but the drawn image is too small:
alt text

here's the code snippet - it saves the bitmap area selected by the user:

  Image OriginalIMG= (Image)((PictureBox)panel1.Controls["picBox"]).Image.Clone()
  Bitmap bmp = new Bitmap(selectedAreaRECT.Width, selectedAreaRECT.Height);
  Graphics g = Graphics.FromImage(bmp);

  g.DrawImage(OriginalIMG, 0,0, selectedAreaRECT, GraphicsUnit.Pixel);
  g.Save();
  g.Dispose();

  bmp.Save(AppDomain.CurrentDomain.BaseDirectory + @"\Temp\" + "img1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

As You see, the code is the same for the img1.jpg from image A and from Image B.
I'm trying to resolve that stupid problem for too long, I don't know what's the reason of that problem. I tried different overloads of the DrawImage method, with no success

EDIT

Resolved! the dafault DPI value of the System.Drawing.Bitmap is = 96, if I open an image with DPI != 96 then the problem described above occurs. To get rid of it, I needed to use SetResolution method:

Bitmap result = new Bitmap(width, height);
result.SetResolution(OriginalIMG.HorizontalResolution, OriginalIMG.VerticalResolution);

that resolves the problem :) Thanks for everyone for help ! :)

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

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

发布评论

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

评论(1

浸婚纱 2024-09-05 20:19:02

我会尝试:
(已编辑)

  g.DrawImage(
    OriginalIMG,
    new Rectangle( Point.Empty, bmp.Size ),
    selectedAreaRECT.X, selectedAreaRECT.Y,
    selectedAreaRECT.Width, selectedAreaRECT.Height, 
    GraphicsUnit.Pixel);

看看是否有影响。

尽管它与您的问题无关:您忘记了 .Dispose() 一些事情,而且我不确定为什么您必须 .Clone() 图像。

I'd try:
(edited)

  g.DrawImage(
    OriginalIMG,
    new Rectangle( Point.Empty, bmp.Size ),
    selectedAreaRECT.X, selectedAreaRECT.Y,
    selectedAreaRECT.Width, selectedAreaRECT.Height, 
    GraphicsUnit.Pixel);

to see if it makes a difference.

Although it has nothing to do with your problem: you're forgetting to .Dispose() some things, and I'm not sure why you have to .Clone() the image.

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