2014-04-15 107 views
0

在一個位圖上,我需要顯示圖形和文本值。所以我做的是用點創建一個位圖,並用這個文本創建另一個位圖並放置在大位圖上。在另一個位圖上覆蓋位圖

我試着用刷子寫文本,但即使設置了透明度,我也無法看到底層圖形。

相反,我想爲文本位圖設置透明度,但我創建的位圖是24位rgb。那麼我們可以設置24位地圖的透明度。

Bitmap textBitmap = null; textBitmap = new Bitmap(10,10,PixelFormat.Format24bppRgb);

using (Graphics memoryGrahics = 
        Graphics.FromImage(textBitmap)) 
    { 

     memoryGrahics.FillRectangle(Brushes.Black, new Rectangle(0, 0, 100, 100)); 
     memoryGrahics.DrawString(result, f, Brushes.White, x, y); 
    } 

    //placing the text bitmap on the graphbitmap  
    using (Graphics g = Graphics.FromImage(GraphBitmap)) 
    { 
     g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; 
     textBitmap.MakeTransparent(); 
     g.DrawImage(textBitmap, 0, 0); 
     return GraphBitmap; 
    } 
+1

你在說什麼類型的位圖? – thumbmunkeys

+0

我想創建的位圖是PixelFormat.Format24bppRgb – Racs

+1

發佈迄今爲止的代碼將有助於徵求答案。 –

回答

0

以及..好像你正在使用2個不同的圖形對象......雖然1個顯卡,1位圖對象可以處理多個定製圖紙佈局,像這樣:

 int width = 800, height = 600; 

     var bit = new Bitmap(width, height); 
     var g = Graphics.FromImage(bit); 

     g.SmoothingMode = SmoothingMode.AntiAlias; 
     g.InterpolationMode = InterpolationMode.HighQualityBicubic; 

     var area = new Rectangle(0, 0, width, height); 
     g.FillRectangle(new LinearGradientBrush(area, Color.PaleGoldenrod, Color.OrangeRed, 45), area); 

     g.DrawImage(Image.FromFile(@"your image"), new Point(10, 10)); 

     g.DrawString("sample", new System.Drawing.Font("Tahoma", 56), new SolidBrush(Color.Black), new PointF(50, 50)); 

     pictureBox1.Image = bit; 

注意g.DrawImage方法也可以用來加載其他位圖