GDI +和大小调整 [英] GDI+ and sizing

查看:121
本文介绍了GDI +和大小调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我在使用GDI +调整位图大小时遇到​​一些奇怪的问题.

说我有一个JPEG图像10000x5000.我用它的原始尺寸显示它的当前零件:

Hi all.
I have some odd problem with resizing bitmaps using GDI+.

Say i have an jpeg image 10000x5000. I display it''s current part in original size with this:

graphics->DrawImage(bitmap, -x, -y, bitmap->GetWidth(), bitmap->GetHeight());



其中x,y是当前移动的坐标.

我正在调整图像的尺寸,如下所示:



where x,y are coordinates of current shifting.

I''m resizing image like this:

Bitmap* newBitmap = new Bitmap((INT)(bitmap->GetWidth()*Scale), (INT)(bitmap->GetHeight()*Scale));
Graphics* graphics = new Graphics(newBitmap);
graphics->ScaleTransform(Scale, Scale);
graphics->DrawImage(bitmap, (INT)0, (INT)0, (INT)(bitmap->GetWidth()), (INT)(bitmap->GetHeight()));



现在的问题是:
当我直接从文件加载图像时,显示非常快速,美观且没有延迟.但是,当我显示调整大小的图像时,即使它变小了,绘图操作也要花很多时间.

我尝试设置不同的InterpolationMode,SmoothingMode,PixelOffsetMode,但没有效果.我试图解决这个问题很长时间.尝试了不同的方法,但是它们都没有效果.也许我缺少明显的东西?也许文件位图"中的某些信息丢失了?像素格式始终相同.好吧,现在我没有别的主意了,所以我希望有人可以帮助我解决我的问题.

忘了说我正在使用普通的winapi,因此.NET解决方案无法帮我.



And now the problem:
When I''m loading image direct from file, displaying works very fast, nice and without delays. But when i display resized image, even if it became smaller, drawing operation takes a lot more time then it was.

I have tried setting different InterpolationMode, SmoothingMode, PixelOffsetMode but it had no effect. I''m trying to manage with this problem for a long time. Tried different ways, but all they had no effect. Maybe I''m missing something obvious? Maybe some information from "file bitmap" gets lost? Pixel format is always the same. Well, now I don''t have any other ideas, so I hope someone can help me with my issue.

Forgot to say that I''m using plain winapi, so .NET solution can''t help me.

推荐答案

也许您可以尝试使用原始图像并调用TranslateTransform().代码看起来像这样(警告:未经测试):
Maybe you could try using the original image and calling TranslateTransform(). The code would look something like this (Warning: not tested):
float width = bitmap->GetWidth()* Scale;
float height = bitmap->GetHeight()* Scale;
 
Graphics* graphics = new Graphics(bitmap);
graphics->TranslateTransform(width , height); 
graphics->ScaleTransform(Scale, Scale);
graphics->DrawImage(bitmap, (INT)0, (INT)0, (INT)width, (INT)height);


两件事:

1-即使您要求提供Image实例,框架也始终会创建一个位图.至少在.NET中是这样.

2-如果您的核心问题是速度问题之一,那么我能看到的唯一可能的解释是您正在泄漏内存. GDI +系统不会制作以某种方式被魔术标记的副本,以使它们使用起来更慢.您是否尝试编写代码来调整图像大小并保存调整后的图像,然后仅加载该图像?我可以看到缩放变换是必须应用的额外图层,它将减慢它的速度,但是,如果您实际上调整图像的大小,只要您通常不放慢速度,那根本就没有任何区别.通过很大比例的内存泄漏导致崩溃.
Two things:

1 - the framework ALWAYS creates a Bitmap, even if you ask for an Image instance. At least, it does in .NET.

2 - If your core issue is one of speed, the only possible explanation I can see is that you''re leaking memory. The GDI+ system does NOT make copies that are somehow magically marked to make them slower to use. Have you tried writing code to resize an image and save the resized image, then just loading that ? I can see how a scale transform is an extra layer that has to be applied, which will slow it down, but if you actually resize the image, that should make no difference at all, so long as you''re not generally slowing things down through a memory leak of significant proportions.


使用PictureBox控件是不可能的吗?因为您可以将Image属性设置为与位图相等,并将PictureBox的SizeMode属性设置为PictureBoxSizeMode.Zoom.然后,您可以调整PictureBox的大小,然后位图将自动调整大小并保持其宽高比.双重缓冲应在调整大小时避免任何麻烦.

我的错.我看到了GDI +,并假设使用Winforms.
Is using a PictureBox control out of the question? Because you can set the Image property equal to the Bitmap and set the PictureBox''s SizeMode property to PictureBoxSizeMode.Zoom. Then you can just resize the PictureBox and the Bitmap will automatically resize and maintain its aspect ratio. Double buffering should take care of any jerkiness during resizing.

My bad. I saw GDI+ and assumed Winforms.


这篇关于GDI +和大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆