2016-03-24 7 views
1

背景色が透明に設定されているPictureBoxコントロールがあります。私はこのコントロール(ソース:https://en.wikipedia.org/wiki/Seven-segment_display#/media/File:7-segment.svg)に画像を描画したいのですが、透明な背景の代わりに背景色は白です(残念ながら、ペイントはアルファチャンネルをサポートしていません)。ここで透明な画像を描画できない

は私がビットマップ描画しようとする方法は次のとおりです。

private void DrawPictureBox() 
{ 
    pbScreen.Image = Update(); 
} 

private Bitmap CreateBackgroundBitmap() 
{ 
    Bitmap bitmap = (Bitmap)Properties.Resources.ResourceManager.GetObject("empty"); 
    bitmap.MakeTransparent(Color.White); 

    return bitmap; 
} 

private ImageAttributes GetImageAttributes() 
{ 
    float[][] matrixItems = { 
     new float[] {1, 0, 0, 0, 0}, 
     new float[] {0, 1, 0, 0, 0}, 
     new float[] {0, 0, 1, 0, 0}, 
     new float[] {0, 0, 0, Contrast, 0}, 
     new float[] {0, 0, 0, 0, 1}}; 

    ColorMatrix colorMatrix = new ColorMatrix(matrixItems); 

    ImageAttributes imageAtt = new ImageAttributes(); 
    imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); 

    return imageAtt; 
} 

private void DrawSegment(ref Graphics g, Digit digit, int Position = 0) 
{ 
    if (digit == null) 
     return; 

    Bitmap buffer = digit.CreateBitmapFromFile(digit.CreateFileNameFromContent()); 
    buffer.MakeTransparent(Color.White); 

    g.DrawImage(buffer, new Rectangle(0 + Position * 43, 0, 43, 67), 0.0f, 0.0f, 43, 67, GraphicsUnit.Pixel, this.GetImageAttributes()); 

} 

public Bitmap Update() 
{ 
    Bitmap buffer = CreateBackgroundBitmap(); 

    Graphics g = Graphics.FromImage(buffer); 

    g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes()); 

    if (State == Powerstate.on) 
    { 
     // Draw segments 
     for (int i = 0; i < digits.Count(); i++) 
     { 
      DrawSegment(ref g, digits[i], i); 

      if (digits[i]?.Dot == Dot.dot_on) 
      { 
       DrawPoint(ref g, digits[i], i); 
      } 
     } 
    } 

    return buffer; 
} 

(ポイントを描画することは似ている)セグメントの描画を期待したが、背景を描画する(GetImageAttributes」により作成されたbeeingてさ、透明性を受け付けないように動作) '。

別の奇妙なことは、私はupdate()メソッド私のGrapics 'G' に

g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes()); 

g.Clear(color: Color.Black); 

を追加した場合、単純に黒であること、である - に関係なく、私が保つか、削除する場合

bitmap.MakeTransparent(Color.White); 

'CreateBackgroundBitmap()'では、黒い背景上にセグメントを描画すると、expとして動作します変わった?

誰かが問題がどこにあるかを見ていますか?何が欠けていますか?

おかげでたくさん:)

答えて

0

まあ、私はそれを得ました。 Graphicsオブジェクトがどのようにビットマップとやりとりし、グラフィックスオブジェクトがどのように作られたのか理解できなかったと思います。ここで

は、それが変更されなければならなかった方法は次のとおりです。

public Bitmap Update() 
{ 
    Bitmap background = new Bitmap(301, 67); 
    Graphics g = Graphics.FromImage(background); 

    g.Clear(color: Color.White); 

    Bitmap buffer = CreateBackgroundBitmap(); 

    g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes()); 

    if (State == Powerstate.on) 
    { 
     for (int i = 0; i < digits.Count(); i++) 
     { 
      DrawSegment(ref g, digits[i], i); 

      if (digits[i]?.Dot == Dot.dot_on) 
      { 
       DrawPoint(ref g, digits[i], i); 
      } 
     } 
    } 

    return background; 
} 
関連する問題