2015-01-31 46 views
0

我有這樣的代碼:如何在新的位圖上繪製已知的矩形大小?

Bitmap newbmp = new Bitmap(512, 512); 

foreach (Point s in CommonList) 
{ 
    w.WriteLine("The following points are the same" + s); 
    newbmp.SetPixel(s.X, s.Y, Color.Red); 
} 

w.Close(); 
newbmp.Save(@"c:\newbmp\newbmp.bmp", ImageFormat.Bmp); 
newbmp.Dispose(); 

的代碼是不是在一個繪畫事件。

然後,我有一個pictureBox1的Paint事件:

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    if (cloudPoints != null) 
    { 
     if (DrawIt) 
     { 
      e.Graphics.DrawRectangle(pen, rect); 
      pointsAffected = cloudPoints.Where(pt => rect.Contains(pt)); 

      CloudEnteringAlert.pointtocolorinrectangle = pointsAffected.ToList(); 
      Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, PixelFormat.Format32bppArgb); 
      CloudEnteringAlert.Paint(e.Graphics, 1, 200, bmp); 
     } 
    } 
} 

Paint事件我畫一個矩形。變量rect。

我想在newbmp上繪製矩形矩形。保存newbmp以在他身上繪製這個矩形。

如何在newbmp上繪製矩形?

+1

您應該創建一個圖形對象:圖形G = Graphics.FromImage(newbmp)){在這裏使用所有的圖形命令!} – TaW 2015-01-31 20:29:56

回答

1

你應該從位圖創建Graphics對象工作它:

.. 
Bitmap newbmp = new Bitmap(512, 512); 
foreach (Point s in CommonList) 
{ 
    Console.WriteLine("The following points are the same" + s); 
    newbmp.SetPixel(s.X, s.Y, Color.Red); 
} 

using (Graphics G = Graphics.FromImage(newbmp)) 
{ 
    G.DrawRectangle(Pens.Red, yourRectangle); 
    .. 
} 

newbmp.Save(@"c:\newbmp\newbmp.bmp", ImageFormat.Bmp); 
newbmp.Dispose(); 

對於這種格式的其他Pen屬性,如WidthLineJoin使用:

using (Graphics G = Graphics.FromImage(newbmp)) 
using (Pen pen = new Pen(Color.Red, 8f)) 
{ 
    // rounded corners please! 
    pen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round; 
    G.DrawRectangle(pen, yourRectangle); 
    //.. 
}