使用asp.net在GDI +中发生了一般错误 [英] A generic error occurred in GDI+ using asp.net
问题描述
我正在尝试从目录中获取所有图像,并逐个在每个图像上放置一个矩形。这是代码
i am trying to get all images from a directory and put a rectangle on each image one by one. here is the code
Brush brush = new SolidBrush(Color.White);
string[] filesindirectory = Directory.GetFiles(("D:/24-8-2015/32373577989/"));
foreach (string item in filesindirectory)
{
Bitmap bitMapImage = new Bitmap(item);
Graphics graphicImage1 = Graphics.FromImage(bitMapImage);
graphicImage1.FillRectangle(brush, 0, 0, 220, 80);
bitMapImage.Save(item, ImageFormat.Png);
graphicImage1.Dispose();
bitMapImage.Dispose();
}
但是当它保存图像时会出现错误
but when it saves the image it gives error at
bitMapImage.Save(item, ImageFormat.Png);
GDI +发生一般性错误。
请告诉我代码中有什么问题。提前感谢
A generic error occurred in GDI+.
please tell me whats wrong in my code. thanks in advance
推荐答案
当您将文件路径传递给位图
构造函数时,文件将保持锁定状态,直到位图
实例被处置:
When you pass a file path to theBitmap
constructor, the file remains locked until theBitmap
instance is disposed:
文件保持锁定,直到处理了位图。
The file remains locked until the Bitmap is disposed.
您正试图在文件仍被锁定时覆盖该文件。
最简单的选项可能是将文件读入内存,将其包装在 MemoryStream
中,然后创建 Bitmap <来自该流的/ code>:
You are trying to overwrite the file while it is still locked.
The simplest option is probably to read the file into memory, wrap it in a MemoryStream
, and create the Bitmap
from that stream:
foreach (string item in filesindirectory)
{
byte[] imageBytes = File.ReadAllBytes(item);
using (Stream stream = new MemoryStream(imageBytes))
using (Bitmap image = new Bitmap(stream))
{
using (Graphics graphics = Graphics.FromImage(image))
{
graphics.FillRectangle(brush, 0, 0, 220, 80);
}
image.Save(item, ImageFormat.Png);
}
}
你还想过滤你正在处理的文件只包括图像:
You'll also want to filter the files you're processing to only include images:
string[] filesindirectory = Directory.GetFiles(@"D:\24-8-2015\32373577989\", "*.png");
这篇关于使用asp.net在GDI +中发生了一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!