簡體   English   中英

wpf創建透明的writeablebitmap

[英]wpf create transparent writeablebitmap

當我這樣創建一個時:

WriteableBitmap wb = new WriteableBitmap(1200, 1600, 90, 90, PixelFormats.Bgr24, null);

當我希望它透明時,它用黑色填充。 我嘗試了Clear(來自WriteableBitmapEx庫),但是嘗試讀取或寫入受保護的內存。 這通常表明其他內存已損壞。“} System.Exception {System.AccessViolationException}

wb.Clear(Colors.Transparent);

有什么想法我可以做到這一點嗎?

編輯:

        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;

使用具有Alpha通道的PixelFormat創建WriteableBitmap,從而允許透明,例如PixelFormats.Bgra32

var wb = new WriteableBitmap(1200, 1600, 96, 96, PixelFormats.Bgra32, null);

現在,每個像素有四個字節,一個為藍色,一個為綠色,一個為紅色,一個為alpha值。

還要注意,通常將DPI參數的值96

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM