0

嗨,我想在 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;
    }
4

1 回答 1

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)

  }

}
于 2013-08-12T19:03:42.980 回答