c# - How to make a CroppedBitmap transparent? -


i have cropped bitmap:

 croppedbitmap crop = new croppedbitmap(rtb, new int32rect(mapoffset, mapoffset, board_width - 1 - mapoffset, board_height - 1 - mapoffset)); 

and need set white transparent. unfortunately, not work (croppedbitmap not contain definition 'maketransparent'):

crop.maketransparent(colors.white); 

anybody have ideas / suggestions?

thanks in advance.

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

i found code how can make white (ffffffff) pixel in image(imagebrush) transparent

but when implement

    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 @ position (0,0).         croppedbitmap crop = new croppedbitmap(rtb, new int32rect(0, 0, board_width - 1, board_height - 1));          writeablebitmap writeable = new writeablebitmap(crop);          // code 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 pixels directly         (int = 0; < n; i++)         {             byte = 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 bmp         bitmapencoder bmpencoder = new bmpbitmapencoder();          bmpencoder.frames.add(bitmapframe.create(writeablebitmap));          //save 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; replaces white black:

enter image description here


edit solution

the solution on here (the problem has wpf , transparency): solution transparency problem

one possible variant create writable bitmap croppedbitmap:

writeablebitmap writeable = new writeablebitmap(cropped); 

then can analyze each pixel of bitmap , change white pixels transparent ones. fastest way pixel buffer using lock method , loop through backbuffer using unsafe code. simpler slower way use copypixels , writepixels methods.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -