Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi..

I'm watermarking images using C# with a .png image.
all the .jpg, .bmp, .png images are getting watermarked perfectly but when watermarking .tif
and .gif images error occurs.

error message : "A Graphics object cannot be created from an image that has an indexed pixel format."

for images other than .tif and .gif
C#
FileStream fs = new FileStream(img, FileMode.Open, FileAccess.Read);
                        image = Image.FromStream(fs);
Graphics imageGraphics = Graphics.FromImage(image);
            watermarkBrush.TranslateTransform(x, y);
            imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(finalWaterImage.Width + 1,finalWaterImage.Height)));

            var filename = Path.GetFileName(img);
            image.Save(outputFolder + "\\" + filename);


for .gif i did this code and it worked :

C#
<pre lang="c#">FileStream fs = new FileStream(CurrentFile, FileMode.Open, FileAccess.Read);
            image = Image.FromStream(fs);

            Bitmap bit = new Bitmap(image.Width, image.Height);
            Graphics newGraphics = Graphics.FromImage((Image)bit);

            newGraphics.DrawImage(image, 0, 0, image.Width, image.Height);
            image = bit;
 Graphics imageGraphics = Graphics.FromImage(image);
            watermarkBrush.TranslateTransform(x, y);
            imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(finalWaterImage.Width + 1,finalWaterImage.Height)));


but this aint working for .tif

the above error occured for .gif images.
now that .if images are working, after passing .tif image i get an error saying "parameter not valid at this line :
C#
image = Image.FromStream(fs);
"
Posted
Updated 6-May-13 19:34pm
v3
Comments
Sergey Alexandrovich Kryukov 6-May-13 2:52am    
This error message is unrelated to the question asked in the title of this post.
If you need any help, you would need to produce some code sample and steps to reproduce the problem. Your problem is in the source file, its pixel format; you should use another one.
—SA
Orcun Iyigun 6-May-13 2:53am    
Can you please post some code? Are you using Image ?
riangel 6-May-13 3:15am    
oh im sorry !! i'll post my code.. asked question for the first time !! :)

Please see my comment to the question. You cannot ask questions this way.

However, I think I know how to solve the problem. My guess is: your exception is thrown in this call:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx[^].

If works for most images, but you happen to come across the image with indexed format, which is not supported (which is quite natural). You were very lucky to catch this case before one of your users did. You should not use it.

How to fix the situation? Before doing this call, you simply need to convert the bitmap to some unified pixel format which is supported by Graphics. The simplest way to do it would be System.Drawing.Bitmap.Clone:
http://msdn.microsoft.com/en-us/library/ms141944.aspx[^],
http://msdn.microsoft.com/en-us/library/ezs7679t.aspx[^].

This should solve your problem. If not, please don't blame me. You should have provided a code sample and a way to reproduce the problem.

—SA
 
Share this answer
 
Comments
riangel 6-May-13 3:51am    
@Sergey Alexandrovich Kryukov i cannot put a restriction on image uploads.
i did it for .gif, now .tif are not working
Sergey Alexandrovich Kryukov 6-May-13 10:29am    
The problem is not directly related with the file type, it is related to pixel format. Did you managed to load a TIFF file at all?
—SA
riangel 7-May-13 0:48am    
@Sergey Alexandrovich Kryukov no.. .tif image is not getting loaded.
it gives error at the filestream itself saying invalid parameter.
Sergey Alexandrovich Kryukov 7-May-13 1:24am    
Then how could you come to your problem? In your question, you reported a different problem. I see a contradiction in your observations as you explained them.
OK, I know what's the problems: please indicate exactly, where the exception was thrown, in what line of your code, to start with.
—SA
riangel 7-May-13 1:35am    
its confusing !!
i'l update my question... :(
Since GDI+ graphics object doesn't support indexed images. I assume you were using Image in your code so that you end up getting that exception. To overcome it you should convert your Image to Bitmap.
C#
try
{
    Image img = Image.FromStream(new MemoryStream(image));
    // OR from file I dont know how you load it
    Bitmap img = new Bitmap(new Bitmap( img ));
    //do your image processing
}
catch (Exception ex)
{
  // do stuff
}

Good luck,
OI
 
Share this answer
 
Comments
riangel 6-May-13 3:19am    
i did this and it worked only for .gif and not for .tif
Thanks :)

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