我对 Graphics.DrawImage 方法有一个非常奇怪的问题。
我在Panel 控件中有PictureBox 控件,其AllowScroll property = true。
该程序根据用户选择的区域将图像切割成小部分。
我加载图像 300x547 并选择区域(红色矩形):
程序正确剪切图像:
然后,我加载另一个图像 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):
program properly cuts the image:
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:
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 ! :)
发布评论
评论(1)
我会尝试:
(已编辑)
看看是否有影响。
尽管它与您的问题无关:您忘记了 .Dispose() 一些事情,而且我不确定为什么您必须 .Clone() 图像。
I'd try:
(edited)
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.