简体   繁体   中英

How to make a CroppedBitmap transparent?

I have a cropped bitmap:

 CroppedBitmap crop = new CroppedBitmap(rtb, new Int32Rect(MapOffset, MapOffset, BOARD_WIDTH - 1 - MapOffset, BOARD_HEIGHT - 1 - MapOffset));

and I need to set white to transparent. Unfortunately, this does not work (CroppedBitmap does not contain a definition for 'MakeTransparent'):

crop.MakeTransparent(Colors.White);

Anybody have any ideas / suggestions?

Thanks in advance.

~~~~~~~~~~~~~~~

I found this code How can I make all white (FFFFFFFF) pixel in an image(ImageBrush) to be transparent

But when I implement it

    public void SaveMainCanvas2BMP(string filename)
    {

        // Write BMP
        VisualBrush sourceBrush = new VisualBrush(MainCanvas);
        DrawingVisual drawingVisual = new DrawingVisual();
        DrawingContext drawingContext = drawingVisual.RenderOpen();
        using (drawingContext)
        {
            drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(- MapOffset, - MapOffset), new Point(BOARD_WIDTH - 1 + MapOffset, BOARD_HEIGHT - 1 + MapOffset)));
        }

        RenderTargetBitmap rtb = new RenderTargetBitmap(BOARD_WIDTH - 1, BOARD_HEIGHT - 1, 96, 96, PixelFormats.Default);
        rtb.Render(drawingVisual);

        //crops  rectangle at position (0,0).
        CroppedBitmap crop = new CroppedBitmap(rtb, new Int32Rect(0, 0, BOARD_WIDTH - 1, BOARD_HEIGHT - 1));

        WriteableBitmap writeable = new WriteableBitmap(crop);

        // Code to turn WHITE pixels TRANSPARENT
        int pixelWidth = (int)writeable.Width;
        int pixelHeight = (int)writeable.Height;
        int Stride = pixelWidth * 4;

        BitmapSource imgSource = (BitmapSource)writeable;
        byte[] pixels = new byte[pixelHeight * Stride];
        imgSource.CopyPixels(pixels, Stride, 0);
        byte TransparentByte = byte.Parse("0");
        byte Byte255 = byte.Parse("255");
        int N = pixelWidth * pixelHeight;
        //Operate the pixels directly
        for (int i = 0; i < N; i++)
        {
            byte a = pixels[i * 4];
            byte b = pixels[i * 4 + 1];
            byte c = pixels[i * 4 + 2];
            byte d = pixels[i * 4 + 3];
            if (a == Byte255 && b == Byte255 && c == Byte255 && d == Byte255)
            {
                pixels[i * 4] = TransparentByte;
                pixels[i * 4 + 1] = TransparentByte;
                pixels[i * 4 + 2] = TransparentByte;
                pixels[i * 4 + 3] = TransparentByte;
            }
        }
        WriteableBitmap writeableBitmap = new WriteableBitmap(pixelWidth, pixelHeight, 96, 96,
            PixelFormats.Pbgra32, BitmapPalettes.Halftone256Transparent);
        writeableBitmap.WritePixels(new Int32Rect(0, 0, pixelWidth, pixelHeight), pixels, Stride, 0);



        //encode as BMP
        BitmapEncoder bmpEncoder = new BmpBitmapEncoder();

        bmpEncoder.Frames.Add(BitmapFrame.Create(writeableBitmap));

        //save to memory stream
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        bmpEncoder.Save(ms);
        ms.Close();
        System.IO.File.WriteAllBytes(filename, ms.ToArray());
    }

It doesn't make WHITE transparent; it replaces WHITE with BLACK:

在此处输入图片说明


EDIT SOLUTION

The solution is over here (the problem has to do with WPF and transparency): Solution to transparency problem

One possible variant is to create a writable bitmap from your CroppedBitmap:

WriteableBitmap writeable = new WriteableBitmap(cropped);

Then you can analyze each pixel of your bitmap and change white pixels to transparent ones. The fastest way to do it is to get the pixel buffer using the Lock method and then loop through the BackBuffer using unsafe code. The simpler but slower way is to use CopyPixels and WritePixels methods.

The problem was with the way WPF handles transparency (BMPs are not the way to go, PNGs are).

Solution here: Solution to transparency problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM