清除图像并在其上绘制信息

时间:2013-08-12 18:13:32

标签: c# drawing

您好我想在c#中绘制一些图像信息。我写了这段代码并且有效:

Bitmap bmp = new Bitmap(@"G:\Cert_template.png");
Graphics g = Graphics.FromImage(bmp);
g.DrawString(cert_id, new Font("B  Zar", 3,System.Drawing.FontStyle.Bold), Brushes.Black, new Point(85, 95));
g.DrawString(date_cert, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(85, 135));
g.DrawString(s1 + s3, new Font("B  Zar", 4, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(90, 290));
g.DrawString(s4, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(480, 360));
g.DrawString(date_exam, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(170, 515));
g.DrawString(Convert.ToString(mark), new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(520, 600));
g.DrawString(lvl, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(150, 600));
g.DrawString(prvnc, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(170, 780));
g.DrawString(center, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(310, 870));
g.DrawString(inst, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(150, 870));
CaptureScreen(g,imgCounter);

我写的所有信息都是一样的。我把这段代码放在一个循环中,然后绘制图像,但是对于不同的信息,它会覆盖以前的图像。我希望清除图像并再次写入,而不会覆盖。

修改

之后,我将g发送到要在图片框中显示的功能:

 private void CaptureScreen(Graphics g,int imgCounter)
    {
        /*This method captures a snapshot of screen and 
         * adds it to the ImageFlowLayoutPanel
         */


        bmp.Save("snap" + imgCounter.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);

        //creating a picturebox control and add it to the flowlayoutpanel
        PictureBox tempPictureBox = new PictureBox();

        //generates a thumbnail image of specified size
        tempPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
        tempPictureBox.Image = bmp.GetThumbnailImage(600, 700,
                               new Image.GetThumbnailImageAbort(ThumbnailCallback),
                               IntPtr.Zero);
        tempPictureBox.Size = new System.Drawing.Size(50,50);

        tempPictureBox.Click += new EventHandler(this.tempPictureBox_Click);
        ImageFlowLayoutPanel.Controls.Add(tempPictureBox);

    }

    //This click event will be used to display the enlarged images
    private void tempPictureBox_Click(object sender, EventArgs e)
    {
        PreviewPictureBox.Image = ((PictureBox)sender).Image;
    }
    public bool ThumbnailCallback()
    {
        return true;
    }

1 个答案:

答案 0 :(得分:0)

一种可能性是基于原始位图创建临时位图, 每个周期。

请注意,我不会立即处理您的Graphics实例 当不再需要时(通过using子句)并注意Graphics实例 是建立在newBitmap之上而不是原始版本之上。

还要确保在循环结束时使用newBitmap引用 而不是仅仅保留原始背景的bmp引用。

Bitmap bmp = new Bitmap(@"G:\Cert_template.png");


for ( ... ) {

  Bitmap newBitmap = new Bitmap(bmp);
  using (Graphics g = Graphics.FromImage(newBitmap)) {

    // somehow alter cert_id, date_cert, etc...

    g.DrawString(cert_id, new Font("B  Zar", 3,System.Drawing.FontStyle.Bold), Brushes.Black, new Point(85, 95));
    g.DrawString(date_cert, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(85, 135));
    g.DrawString(s1 + s3, new Font("B  Zar", 4, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(90, 290));
    g.DrawString(s4, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(480, 360));
    g.DrawString(date_exam, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(170, 515));
    g.DrawString(Convert.ToString(mark), new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(520, 600));
    g.DrawString(lvl, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(150, 600));
    g.DrawString(prvnc, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(170, 780));
    g.DrawString(center, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(310, 870));
    g.DrawString(inst, new Font("B  Zar", 3, System.Drawing.FontStyle.Bold), Brushes.Black, new Point(150, 870));

    // and here you do something with your newBitmap instance
    // (for example: you save it to another filename)

  }

}