bitmap.save函数gdi错误

编程入门 行业动态 更新时间:2024-10-28 07:25:18
本文介绍了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错误

本文发布于:2023-07-26 19:03:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1217910.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   错误   bitmap   save   gdi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!