Groups | Blog | Home
all groups > dotnet drawing api > may 2005 >

dotnet drawing api : Error in Replace Colors In Image and Save it!



Prasad Dannani
5/28/2005 12:00:00 AM
Hi,

I created a Windows Application which accepts the filename to open and two
color values.

The Application opens the given bitmap file and replaces the first color
with second color. I done by traversing through the pixels.

After completing the whole process i have to save the image to new/existing
file. I am getting an error "System.ArgumentException: Invalid parameter
used"

Image objSelImage = new Image("User selected filename");
Bitmap bmp1 = new Bitmap( objSelImage ); //Convert to bitmap object
Bitmap bmp2; //Bitmap Object to Save
bmp2 = ReplaceColors.ReplaceColor(bmp1, oldcolor, newcolor);
//ReplaceColor is the function written by me to replace the colors. which
will take the existing bitmap object and return the one after replacing the
color.
bmp1.Dispose(); //Closing the first image object.
bmp2.Save("c:\\Sample.gif"); //The new image object i'm saving it to a
file.

To Clear this error i tried all possible ways i was taken only gif file and

System.IO.FileStream fs= new
System.IO.FileStream(saveFileDialog1.FileName,System.IO.FileMode.Create);
bmp2.Save(fs,System.Drawing.Imaging.ImageFormat.Gif);
bmp2.Dispose();
fs.Close();

Can anyone tell how to clear this error. and Suggest the better way to
replace the colors I have seen some codes which are doingit through
colormaps but i didn't understand how to save that.

Thanks in Advance
Prasad Dannani.

Morten Wennevik
5/29/2005 12:00:00 AM
Hi Prasad,

You have a few oddities in your code. For instance, you cannot create a 'new' Image object
You would do

Image img = Image.FromFile(filepath)

However, this is really a Bitmap object so you don't need to create a new Bitmap.

Bitmap bmp1 = (Bitmap)img;

Anyhow, I suspect the error lies inside your ReplaceColors class, and since you aren't showing us that bit we can't help you.

If the goal is to change pixel colors of a gif file (indexed file), then you can simply change the color palette

In theory this code should work

Bitmap bmp1 = (Bitmap)Image.FromFile("c:\\red2.gif");
ColorPalette pal = bmp1.Palette;

for(int i = 0; i < pal.Entries.Length; i++)
{
if(pal.Entries[i] == oldcolor)
pal.Entries[i] = newcolor;
}
bmp1.Palette = pal;

However, for some odd reason the gif still displays the old colors.


--
Happy coding!
AddThis Social Bookmark Button