3

色の値BitmapImage に基づいて作成する方法がわかり ません。

たとえば、文字列「Black」があるため、黒のBitmapImageが必要です。

どのようにそれを行うことができますか?

ありがとうございました!

-- 更新 ( @StephenChungにこのコードを入れました)

アイデアは、任意の不透明度で Grid を実行し、その子に不透明度を適用しないことです。そのため、必要な色で画像を作成し、不透明度を適用します。

BitmapSource bs = CreateBitmapSource(GetBackgroundColorValue());
// and here I use method of @StaWho CreateBitmapSource()
ImageBrush ib2 = new ImageBrush(bs);
ib2.Opacity = Opacity;
ib2.Stretch = Stretch.Fill;
RootGrid.Background = ib2;
4

2 に答える 2

4

ImageBrush以下から作成できますBitmapSource

    private BitmapSource CreateBitmapSource(System.Windows.Media.Color color)
    {
        int width = 128;
        int height = width;
        int stride = width / 8;
        byte[] pixels = new byte[height * stride];

        List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
        colors.Add(color);
        BitmapPalette myPalette = new BitmapPalette(colors);

        BitmapSource image = BitmapSource.Create(
            width,
            height,
            96,
            96,
            PixelFormats.Indexed1,
            myPalette,
            pixels,
            stride);

        return image;
    }
于 2012-05-17T14:18:57.113 に答える
0

例:

System.Drawing.Bitmap flag = new System.Drawing.Bitmap(10, 10);
for( int x = 0; x <  flag.Height; ++x )
    for( int y = 0; y < flag.Width; ++y )
        flag.SetPixel(x, y, Color.Black);
for( int x = 0; x < flag.Height; ++x )
    flag.SetPixel(x, x, Color.Black);

そして変換:

private BitmapImage Bitmap2BitmapImage(Bitmap bitmap) 
{ 
    using (MemoryStream ms = new MemoryStream()) 
    { 
        bitmap.Save(ms, ImageFormat.Png); 
        ms.Position = 0; 
        BitmapImage bi = new BitmapImage(); 
        bi.BeginInit(); 
        bi.StreamSource = ms; 
        bi.EndInit(); 

        return bi; 
    } 
} 
于 2012-05-17T14:15:18.963 に答える