【发布时间】:2019-04-12 03:08:14
【问题描述】:
我正在编写一个应用程序来使用一些中间位图为我绘制一些图像,但是如果我之前处理用于调整图像大小的中间位图,我会从 GDI+(图形类)收到无效参数错误绘制位图。
应用程序本身旨在获取输入图像并将其裁剪并调整大小后输出为图像文件。现在这意味着在某个时间点,调整大小的图像存储在中间位图中。调整大小完成后,中间位图被分配给原始位图,然后使用 GDI+ 进行绘制,中间位图被处理掉。但是由于某种原因,如果在重新绘制原始图像之前处理掉了中间体(尽管它的值被分配给另一个变量并且理论上不需要),程序会抛出一个无效参数错误,我很好奇为什么。
此版本抛出无效参数错误
using system.drawing;
Bitmap Background = new Bitmap(*filepath*);
Bitmap Image = new Bitmap(*another filepath*);
Bitmap ResizedImage = new Bitmap(825, 1125);
...Some code that clips and resizes Image and draws it onto ResizedImage...
Image = ResizedImage;
ResizedImage.Dispose();
using(Graphics g = Graphics.FromImage(Background)
{
g.DrawImage(Image, 0, 0, 825, 1125);
}
但是这个版本没有,只有一行的位置改变
using system.drawing;
Bitmap Background = new Bitmap(*filepath*);
Bitmap Image = new Bitmap(*another filepath*);
Bitmap ResizedImage = new Bitmap(825, 1125);
...Some code that clips and resizes Image and draws it onto ResizedImage...
Image = ResizedImage;
using(Graphics g = Graphics.FromImage(Background)
{
g.DrawImage(Image, 0, 0, 825, 1125);
ResizedImage.Dispose();
}
由于 ResizedImage 在处理之前已将其内容复制到 Image 中,因此 Image 应该可以正常绘制,但如果我使用代码的第一位,我会收到无效参数错误,我只是想知道原因。我猜这与位图分配的方式有关
【问题讨论】: