保存图像位图时出错 GDI+ 中发生一般错误
Getting Error when to save image bitmap A generic error occurred in GDI+
下面给出了我的代码,我想做的是从项目文件夹中获取图像,然后在图像上添加一些文本,然后将其保存到同一文件夹中。
string firstText = "Hello";
string secondText = "World";
PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);
var imageFilePath = Server.MapPath("~/Images/" + "a.png");
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (Font arialFont = new Font("Arial", 10))
{
graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
}
}
bitmap.Save(imageFilePath);//save the image file
我认为您正在尝试覆盖当前打开的图像文件:
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file
您可以做的是创建一个单独的 bitmap
实例,关闭源代码,然后再将其保存到同一文件。
这里有一段代码可以参考:
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);
Bitmap temp = new Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat); //Create temporary bitmap
using (Graphics graphics = Graphics.FromImage(temp))
{
using (Font arialFont = new Font("Arial", 10))
{
//Copy source image first
graphics.DrawImage(bitmap, new Rectangle(0, 0, temp.Width, temp.Height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
}
}
bitmap.Dispose(); //Dispose your source image
temp.Save(imageFilePath);//save the image file
temp.Dispose(); //Dispose temp after saving
下面给出了我的代码,我想做的是从项目文件夹中获取图像,然后在图像上添加一些文本,然后将其保存到同一文件夹中。
string firstText = "Hello";
string secondText = "World";
PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);
var imageFilePath = Server.MapPath("~/Images/" + "a.png");
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (Font arialFont = new Font("Arial", 10))
{
graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
}
}
bitmap.Save(imageFilePath);//save the image file
我认为您正在尝试覆盖当前打开的图像文件:
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file
您可以做的是创建一个单独的 bitmap
实例,关闭源代码,然后再将其保存到同一文件。
这里有一段代码可以参考:
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);
Bitmap temp = new Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat); //Create temporary bitmap
using (Graphics graphics = Graphics.FromImage(temp))
{
using (Font arialFont = new Font("Arial", 10))
{
//Copy source image first
graphics.DrawImage(bitmap, new Rectangle(0, 0, temp.Width, temp.Height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
}
}
bitmap.Dispose(); //Dispose your source image
temp.Save(imageFilePath);//save the image file
temp.Dispose(); //Dispose temp after saving