Reputation: 1106
When I create one like this:
WriteableBitmap wb = new WriteableBitmap(1200, 1600, 90, 90, PixelFormats.Bgr24, null);
Its filled with black when I want it to be transparent. I tried Clear(from WriteableBitmapEx library) but I get a Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} System.Exception {System.AccessViolationException}
wb.Clear(Colors.Transparent);
Any ideas how I could accomplish this?
Edit:
List<FormattedText> text = new List<FormattedText>();
WriteableBitmap wb = new WriteableBitmap(1200, 1600, 96, 96, PixelFormats.Bgr32, null);
wb.Clear(Colors.Transparent);
TransformedBitmap tb = new TransformedBitmap(wb, new RotateTransform(0));
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawImage(tb, new Rect(0, 0, tb.PixelWidth, tb.PixelHeight));
for (int i = 0; i < text.Count; i++)
{
drawingContext.DrawText(text[i], points[i]);
drawingContext.DrawEllipse(null, new Pen(new SolidColorBrush(Colors.Aqua), 3), points[i], 10, 10);
}
drawingContext.Close();
System.Windows.Media.Imaging.RenderTargetBitmap bmp = new System.Windows.Media.Imaging.RenderTargetBitmap(tb.PixelWidth, tb.PixelHeight, 96, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
Image = bmp;
Upvotes: 1
Views: 3032
Reputation: 128013
Create the WriteableBitmap with a PixelFormat that has an alpha channel and thus allows for transparency, e.g. PixelFormats.Bgra32
:
var wb = new WriteableBitmap(1200, 1600, 96, 96, PixelFormats.Bgra32, null);
Now you have four bytes per pixel, one for blue, one for green, one for red, and one for the alpha value.
Note also that you would usually use a value of 96
for the DPI parameters.
Upvotes: 2