簡體   English   中英

如何在 WindowsForms Picturebox 上繪制字符串?

[英]How to draw strings on WindowsForms Picturebox?

我有自己的簡單 3d 引擎類 ( myGraphics ),它將結果保存myGraphics圖變量中。 然后將該位圖放置在 PictureBox 中( PictureBox.Image = myGraphics.bmp

這運行良好,當我需要在繪圖上標記一些點時出現問題。 由於沒有myGraphics圖上繪制字符串的內置方法,我遵循了類似問題中的解決方案,現在我的myGraphics上有一個Graphics變量,因此我可以在那里繪制數字。 代碼的重要部分如下:

public class myGraphics
{
    public Graphics g;
    public void initialize(int W, int H )
    {
        //sets some values 
        g = Graphics.FromImage(bmp);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
    }
    public void draw()
    {
        // code to draw on myGraphics.bmp
        markpoints();
    }
    public void markpoints() 
    {
        if (draw_points) { 
            foreach (objecto ob in solidos) {
                for (int p = 0; p < ob.viewpoints.Count; p++)
                {
                    // determine position of text (x, y)
                    SolidBrush drawBrush = new SolidBrush(Color.Black);
                    g.DrawString(p.ToString(), new Font("Arial", 10), drawBrush, x, y);
                    g.Flush();
                }
            }
        }
    }

現在,如果我做對了,g.Flush() 應該合並位圖上的 Graphics g,但它不是那樣工作的,所以我得到了正確的圖像但沒有刺痛。

我也試過這個: myGraphics.g = PictureBox.CreateGraphics(); 來解決它。 在調試模式下使用斷點我意識到字符串確實出現在使用此方法的控件上,但在 PictureBox 更新后立即被刪除。

那么,我該如何解決這個問題,這樣我才能得到圖紙上顯示的數字?

PictureBox.Image更新時,圖形將被清除。 因此,必須在更新PictureBox.Image后立即從類外部調用markpoints()

此外,分配myGraphics.g = Picturebox.CreateGraphics()不起作用,myGraphics.g 上的更改不會更新到控件 PictureBox。 控件的圖形必須在繪制事件時更新,將e.graphics傳遞給markpoints

此外, g.flush()不會將圖形與 Bitmap 合並,這段代碼實際上是不必要的

所以,最終的解決方案:

public class myGraphics
{
    // public Graphics g is no longer necessary
    public void initialize(int W, int H ) { //sets some values}
    public void draw() { // code to draw on myGraphics.bmp }

    public void markpoints(Graphics graph) 
    {
        if (draw_points) { 
            graph.SmoothingMode = SmoothingMode.AntiAlias;
            graph.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graph.PixelOffsetMode = PixelOffsetMode.HighQuality;

            foreach (objecto ob in solidos) {
                for (int p = 0; p < ob.viewpoints.Count; p++)
                {
                    // determine position of text (x, y)
                    SolidBrush drawBrush = new SolidBrush(Color.Black);
                    g.DrawString(p.ToString(), new Font("Arial", 10), drawBrush, x, y);
                }
            }
        }
    }
}

// Connect to picture box
myGraphics mg = new myGraphics()
Picturebox.Image = mg.bmp;
PictureBox.Invalidate(); // forces update of the control using paint event

private void PictureBox_Paint(object sender, PaintEventArgs e) 
{
    mg.markpoints(e.Graphics);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM