bitmap.save函数gdi错误 [英] bitmap.save function gdi error

查看:76
本文介绍了bitmap.save函数gdi错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个代码.
1.这是预览:

I have 2 code.
1. This is for preview :

Bitmap gambarBaru = new Bitmap(picOriginal.Image);
Graphics gfxGambar = Graphics.FromImage(gambarBaru);
Size sizeGambar = picOriginal.Image.Size;
Rectangle rctGambr = new Rectangle(new Point(0,0),sizeGambar);
ImageAttributes attrGambar = new ImageAttributes();
attrGambar.SetColorMatrix(colorTransformer, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
gfxGambar.DrawImage(picOriginal.Image, rctGambr, 0, 0, sizeGambar.Width, sizeGambar.Height, GraphicsUnit.Pixel, attrGambar);
attrGambar.Dispose();
gfxGambar.Dispose();
picAfter.Image = gambarBaru;
string apploc = System.IO.Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();
gambarBaru.Save(apploc + "\\config.png");



此代码可以成功运行.一会儿,
2.转换代码:



This code can run successfully. While,
2. The code for conversion :

private Bitmap TransformImage(Bitmap originalImage)
{
Bitmap gambarBaru = new Bitmap(originalImage.Width, originalImage.Height);  
Graphics gfxGambar = Graphics.FromImage(gambarBaru);
Size sizeGambar = gambarBaru.Size;
Rectangle rctGambr = new Rectangle(new Point(0, 0), originalImage.Size);
ImageAttributes attrGambar = new ImageAttributes();
attrGambar.SetColorMatrix(colorTransformer, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
gfxGambar.DrawImage(originalImage, rctGambr, 0, 0, sizeGambar.Width, sizeGambar.Height, GraphicsUnit.Pixel, attrGambar);
attrGambar.Dispose();
gfxGambar.Dispose();
return gambarBaru;
}
        
private void ColorizeImageFile(string imagePath, string TargetImagePath, ImageFormat format)
{
Bitmap originalBitmap = new Bitmap(imagePath, true);
Bitmap targetBitmap = TransformImage(originalBitmap);
originalBitmap.Dispose();
targetBitmap.Save(TargetImagePath,ImageFormat.Png);
}


此代码始终会引发GDI错误异常.错误发生在targetBitmap上.有人知道为什么会这样吗?


this code always throw GDI error exception. The error is on targetBitmap. Anyone knows why is this happen?

推荐答案

这是一个熟悉的问题.如果要使用PNG格式保存图像,则需要将位图保存到MemoryStream对象,并使用FileStream
MemoryStream写入硬盘.
祝你好运,

爱德华
This is a familiar problem. If you want to save the image using the PNG format, you need to save the bitmap to a MemoryStream object and write that MemoryStream to your harddrive using a FileStream

Good Luck,

Eduard


最后,我知道了造成此问题的原因.它位于TargetImagePath上.在代码#1上,路径总是正确形成的(实际上并非总是:D).代码#2,必须提供正确格式的路径.就我而言,我只是希望Bitmap.Save()仅创建文件夹(如果尚未创建).这是错误的原因.您必须使用Directory.CreateDirectory()手动创建文件夹(如果尚不存在)
At last, I know the causes of this problem. It''s on the TargetImagePath. On the code #1, the path always formed properly (actually not always :D). The code #2, you must supply the correctly formed path. In my case, I just expect Bitmap.Save() to just create the folder if not already created. This is the cause of error. You must create the folder manually using Directory.CreateDirectory() if not already present


这篇关于bitmap.save函数gdi错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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