locked
WPF Image Background issue RRS feed

  • Question

  • HI,

    I have BitmapSource from physical image.

    private void RenderImage(BitmapSource source, DrawingContext dc, Rect imageRect, bool isPrinting)
            {

    I use Image object to create Image from bitmapsource:

     Image bgImage = new Image() { Source = source };

    }

    Is there any way I can find out that my image is transparent of it has white background. There are 1000 images in my project and I want to find out for each.

    I also have to make sure that solution of this wont cause performance issue.

    Thanks

    Dee

    Tuesday, February 19, 2013 10:02 PM

Answers

  •             for (int i = 0; i < bitmap.PixelWidth; i++)
                {
                    for (int j = 0; j < bitmap.PixelHeight; j++)
                    {
                        var pixel = GetPixel(bitmap, i, j);
                        if (pixel.A == 0)
                        {
                            isTransparent = true;
                            break;
                        }
                    }
                    if (isTransparent)
                        break;
                }
    • Marked as answer by Dee Choksi Thursday, February 28, 2013 2:18 PM
    Wednesday, February 20, 2013 6:11 PM

All replies

  • you can create a loop to go through each pixel in the image and check the alpha value?  not sure on the performance of this though.  but that will tell you if the image uses any transparency.  to tell if the image supports transparency then you can use

    if(bgImage.Flags & 0x2) != 0) {}

    Tuesday, February 19, 2013 10:13 PM
  • Can you please share some example for that
    Wednesday, February 20, 2013 3:30 AM
  • There is nothing like Flags property of Image. Can you please share some examples.
    Wednesday, February 20, 2013 3:33 AM
  •             for (int i = 0; i < bitmap.PixelWidth; i++)
                {
                    for (int j = 0; j < bitmap.PixelHeight; j++)
                    {
                        var pixel = GetPixel(bitmap, i, j);
                        if (pixel.A == 0)
                        {
                            isTransparent = true;
                            break;
                        }
                    }
                    if (isTransparent)
                        break;
                }
    • Marked as answer by Dee Choksi Thursday, February 28, 2013 2:18 PM
    Wednesday, February 20, 2013 6:11 PM
  • Hi Dee Choksi,

    If you have resolved your issue, I think your code above is correct.

    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 21, 2013 7:52 AM