Skip to main content
 首页 » 编程设计

c#之图形之在图片框上键入文本

2024年02月13日16mfrbuaa

有没有办法在 PictureBox 或 Panel 上输入/编辑文本?当用户单击 PictureBox 上的某处时,将显示插入符号并允许用户键入文本。我正在制作一个关于图形的程序。我希望它像 MS Paint 或 Photoshop 中的文本工具一样。但我不知道。有人对此有经验请给我指导。

请您参考如下方法:

这里有一些代码,您可以使用这些代码来让它为您工作:

private void image_Click(object sender, EventArgs e) 
    { 
        float y = (float)Cursor.Position.Y -50; 
        float x = (float)Cursor.Position.X -50; 
 
        Bitmap b = new Bitmap(@"C:\Users\Dozer789\Downloads\notepad-png.bmp"); 
 
        RectangleF r = new RectangleF(x, y, 0, 0); 
 
        Graphics g = Graphics.FromImage(b); 
 
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 
        g.DrawString("yourText", new Font("Thaoma", 8), Brushes.Black, r); 
 
        g.Flush(); 
 
        image.Image = b; 
    } 

您将不得不编辑和添加一些内容,但这至少是一个开始。

希望这有用!!