Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");


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);
}

this code always throw GDI error exception. The error is on targetBitmap. Anyone knows why is this happen?
Posted
Comments
Olivier Levrey 8-Mar-11 9:56am    
Which line throws the exception and what is the exception message?
asugix 8-Mar-11 21:21pm    
the error is thrown on function : ColorizeImage on code : targetBitmap.Save
When I check using pop-up-debugger view, many properties on targetBitmap throwing exceptions just after that code.
Ryan Zahra 8-Mar-11 10:02am    
Put a try catch block. See the error message and reply back.
asugix 8-Mar-11 21:24pm    
I'm not putting try catch. Instead I'm put breakpoint on "Bitmap originalBitmap = new Bitmap(imagePath, true);", then step through the code.

everything is okay before targetBitmap.Save. After executing that line, it says "GDI error". When I check on the targetBitmap properties, almost all of them throwing exceptions
Albin Abel 8-Mar-11 10:17am    
looks like the error might occurs here
attrGambar.SetColorMatrix(colorTransformer, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

other things looks ok. You haven't shown here the colorTransformer matrix

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
 
Share this answer
 
Comments
fjdiewornncalwe 8-Mar-11 17:06pm    
+5. Works for me that way.
asugix 8-Mar-11 21:28pm    
if I save the image just like the code #1. It's okay. But when using code #2 it have error.
I think the problem because I'm returning Bitmap object from TransformImage(Bitmap originalImage) and then excute Bitmap.Save

that is what I want to know why.
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900