【发布时间】:2019-12-13 17:56:58
【问题描述】:
我正在尝试使用 Bitmap 在 c# 中将多个图像合并为一个,根据我的代码,我的图像宽度为 5000,高度为 1800(这是恒定的)我最多可以组合 12 个图像(总宽度将是 60000 和 1800 高度)。我总共有 68 张图像要组合。保存图像时出现“通用错误”(尝试组合超过 12 张图像时发生错误)。 最终输出应该是 68 张图片与(350000 宽度* 1800 高度)的组合。
我尝试过使用Bitmap,有没有办法使用WIC(Windows成像组件)来组合多个图像。
class Program
{
static void Main(string[] args)
{
int width = 0;
int height = 0;
List<Image> images = new List<Image>();
var folderPath = "C:\\home\\imagepath";//contains all the images
var file = Directory.GetFiles(folderPath, "*.png");
String jpg3 = @"finalimage.png";
var path = "C:\\home\\data\\FinalImage";
foreach (var item in file)
{
Image image1 = Image.FromFile(item);
images.Add(image1);
width += image1.Width; // adding all the images width
height = image1.Height;
}
Bitmap finalImage = new Bitmap(width, height, PixelFormat.Format32bppArgb);
finalImage.MakeTransparent();
Graphics g = Graphics.FromImage(finalImage);
// g.Clear(SystemColors.AppWorkspace);
g.Clear(Color.White);
var nextwidth = 0;
foreach (var img in images)
{
g.DrawImage(img, new Point(nextwidth, 0));
nextwidth += img.Width;
}
g.Dispose();
foreach (var img in images)
{
img.Dispose();
}
finalImage.Save(Path.Combine(path, jpg3), ImageFormat.Png);// getting error when combining more than 12 images and saving in particular folder
finalImage.Dispose();
}
}
“错误:System.Runtime.InteropServices.ExternalException:'一个通用的 GDI+ 中发生错误。'"
在finalImage.save(path.combine(path,jpg3),ImageFormat.Png); 上收到此错误
【问题讨论】:
-
该图像的大小将接近 2GB(假设 3 字节/像素),因此它可能太大了。您可能需要使用Windows Imaging Component 来执行此操作(通过
System.Windows.Media.Imaging) -
你能分享一个相同的示例链接吗?
-
恐怕我没有指向使用 WIC 组合图像的示例应用程序的链接
-
您是作为 32 位进程运行的吗?这永远不会起作用(图像> 2Gb)。尝试 64 位。否则尝试 WPF:这个
new WriteableBitmap(350000, 1800, 96, 96, PixelFormats.Bgra32, null);以 64 位运行。 -
@SimonMourier 我在 64 位进程中运行它,即使在使用 WriteableBitmap 之后我在尝试保存图像时收到错误“图像尺寸超出此编解码器支持的范围”