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?