Quantcast
Channel: WriteableBitmapEx
Viewing all articles
Browse latest Browse all 360

New Post: Alpha channel problem

$
0
0
I modified alpha channel directory. but i got strange result

caseA : Modified by WriteableBitmapEx drawing function -> OK (half side completely transparent)
caseB : Modified alpha channel directory (WriteableBitmapEx) -> NG (half side not completely transparent)
caseC : Modified alpha channel directory (Normal WriteableBitmap) ->OK (half side completely transparent)

what happned ?
WriteableBitmapEx vertion is 1.0.12.0
    public partial class MainWindow : Window {
        WriteableBitmap chessBoard;
        WriteableBitmap bitmap1;
        WriteableBitmap bitmap2;
        WriteableBitmap bitmap3;
        public MainWindow() {
            InitializeComponent();
            chessBoard = MakeChessBordBitmap(100, 100, 5);
            bitmap1 = BitmapFactory.New(100, 100);
            bitmap2 = BitmapFactory.New(100, 100);
            bitmap3 = new WriteableBitmap(100, 100, 72, 72, PixelFormats.Pbgra32, null);

            using (bitmap1.GetBitmapContext()) {
                bitmap1.Clear(Color.FromArgb(255, 255, 0, 0));
                bitmap1.FillRectangle(0, 0, 100, 50, Color.FromArgb(0, 0, 0, 0));
            }
            using (bitmap2.GetBitmapContext()) {
                bitmap2.Clear(Color.FromArgb(255, 0, 255, 0));
            }
            unsafe {
                bitmap2.Lock();
                for (int y = 0; y < 50; y++) {
                    for (int x = 0; x < 100; x++) {
                        byte* p = (byte*)bitmap2.BackBuffer + bitmap2.BackBufferStride * y + x * 4;
                        *(p + 3) = 0; // Alpha
                    }
                }
                bitmap2.AddDirtyRect(new Int32Rect(0, 0, 100, 100));
                bitmap2.Unlock();
            }

            unsafe {
                bitmap3.Lock();
                for (int y = 0; y < 100; y++) {
                    for (int x = 0; x < 100; x++) {
                        byte* p = (byte*)bitmap3.BackBuffer + bitmap3.BackBufferStride * y + x * 4;
                        if (y < 50) {
                            *(p++) = 0; // B
                            *(p++) = 0; // G
                            *(p++) = 0; // R
                            *(p++) = 0; // Alpha
                        }
                        else {
                            *(p++) = 255; // B
                            *(p++) = 0; // G
                            *(p++) = 0; // R
                            *(p++) = 255; // Alpha
                        }
                    }
                }
                bitmap3.AddDirtyRect(new Int32Rect(0, 0, 100, 100));
                bitmap3.Unlock();
            }
            caseA1.Source = chessBoard;
            caseA2.Source = bitmap1;

            caseB1.Source = chessBoard;
            caseB2.Source = bitmap2;

            caseC1.Source = chessBoard;
            caseC2.Source = bitmap3;
        }
        // ChessBord
        WriteableBitmap MakeChessBordBitmap(int w, int h, int n) {
            WriteableBitmap bitmap = BitmapFactory.New(w, h);
            int cx = w / n;
            int cy = h / n;
            int i, j;
            int x2, y2;
            using (bitmap.GetBitmapContext()) {
                bitmap.Clear(Colors.White);
                for (i = 0; i < n; i++) {
                    for (j = 0; j < n; j++) {
                        if ((i + j) % 2 == 0) {
                            y2 = Math.Min(cy * i + cy - 1, h - 1);
                            x2 = Math.Min(cx * j + cx - 1, w - 1);
                            bitmap.FillRectangle(j * cx, i * cy, x2, y2, Colors.Gray);
                        }
                    }
                }
            }
            return bitmap;

        }
    }
<Window x:Class="AlphaBug.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="640">
    <StackPanel Orientation="Horizontal">
        <Viewbox Width="200" Height="200">
            <Grid>
                <Image x:Name="caseA1"/>
                <Image x:Name="caseA2"/>
            </Grid>
        </Viewbox>
        <Viewbox Width="200" Height="200" Margin="10,0,0,0">
            <Grid>
                <Image x:Name="caseB1"/>
                <Image x:Name="caseB2"/>
            </Grid>
        </Viewbox>
        <Viewbox Width="200" Height="200" Margin="10,0,0,0">
            <Grid>
                <Image x:Name="caseC1"/>
                <Image x:Name="caseC2"/>
            </Grid>
        </Viewbox>
    </StackPanel>
</Window>

Viewing all articles
Browse latest Browse all 360

Trending Articles