-4

I currently am using a function that checks whether or not an image is a certain size and if so, I convert it to a jpeg (example: if it is a large png, convert to jpeg).

I was wondering if there was a C# function that allowed you to check for transparency. If the image is transparent, then keep it transparent and don't convert.

Any help in the right direction would be great. Thanks!

gunr2171
  • 16,104
  • 25
  • 61
  • 88
james
  • 5,006
  • 8
  • 39
  • 64
  • 2
    please show some source code... what have you tried ? what is not working ? – Yahia May 10 '13 at 20:23
  • 2
    There is another answer to the same question here -> http://stackoverflow.com/questions/2569538/detecting-if-a-png-image-file-is-a-transparent-image - its not fast though... – Darren Wainwright May 10 '13 at 20:25
  • 1
    Use [ImageResizer](http://imageresizing.net/). I started using this recently and I'll never write a resizer/cropper function again (still in the process of phasing out my old functions tho). It's awesome. – MikeSmithDev May 10 '13 at 20:37

1 Answers1

1

I haven't tested, but maybe this snippet will work:

System.Drawing.Image myImage; //Set source from image here
System.Drawing.Bitmap myBitmap = new System.Drawing.Bitmap(myImage);

for (xPixel = 0; xPixel <= (myBitmap.Width - 1); xPixel++) {
    for (yPixel = 0; yPixel <= (myBitmap.Height - 1); yPixel++) {
        if (myBitmap.GetPixel(xPixel, yPixel) == Drawing.Color.Transparent) {
            // Image contains transparency
        }
    }
}
J.Hudler
  • 1,238
  • 9
  • 26
  • Thanks for the responses. I will certainly try J. Hudler and MikeSmith's suggestions on Monday when I get into work. I also checked out the link that was provided. Thank you all for your answers, I'll "vote" once I can test it all out! – james May 12 '13 at 19:25
  • This is awfully slow! – bitbonk Jan 24 '14 at 16:12
  • It also won't detect partial transparency. You have to check the colour's alpha component to see if it is smaller than 255. There are also a bunch of preliminary checks you can do in single commands to see if the image has an alpha channel at all. – Nyerguds Aug 18 '16 at 08:46