在PictureBox上显示文字(用label或textbox背景色会有问题吧)

    最近的一个项目要在一张图上显示一个句子,先用了个PictureBox,然后往上面拽了个textbox,没想到,将textbox的backcolor设置为transparent,根本没有任何效果,图上文字底色还是白的。网上有说还要再把textbox的parent属性设为picturebox,但我的项目是PDA上的,用compack framefork,不支持这个操作,会发生运行错。

    于是用了下面的方法,从Image中得到graphics对象,然后用它画string,感觉效果还可以,下面是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DrawTextOnPicture
{
    public partial class DrawTextOnPicture : Form
    {
        public DrawTextOnPicture()
        {
            InitializeComponent();
            bFlag = true;
            LoadBitmap();
        }

        private void LoadBitmap()
        {
            Rectangle drawRect = new Rectangle();

            drawRect.X = pbFile.Location.X - pbFile.Width / 4;
            drawRect.Y = pbFile.Location.Y + pbFile.Height / 4;
            drawRect.Width = labelHide.Width;
            drawRect.Height = labelHide.Height;

            Image fileImage = new Bitmap(@"Program Files/DrawTextOnPicture/file.jpg");
            Graphics g = Graphics.FromImage(fileImage);

            g.DrawString(labelHide.Text, labelHide.Font, new SolidBrush(labelHide.ForeColor), drawRect);
            pbFile.Image = fileImage;
        }

        private void btnChangeText_Click(object sender, EventArgs e)
        {
            if (bFlag)
            {
                labelHide.Text = "您好 我的主人";
                bFlag = !bFlag;
            }
            else
            {
                labelHide.Text = "Hello lcrystal";
                bFlag = !bFlag;
            }

            LoadBitmap();
        }
    }
}

效果就是按一下按钮,换个显示。变量如下:

private System.Windows.Forms.PictureBox pbFile;
private System.Windows.Forms.Button btnChangeText;
private System.Windows.Forms.Label labelHide;
private bool bFlag;

我会把程序放到我的空间里的,大家可以下载看看。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一些基本步骤,帮助您实现在OpenCvSharp窗体中导入一张图片,在TextBox中输入文字,在另一个PictureBox上生成TextBox中的文字的详细代码: 1. 在窗体中添加两个PictureBox控件和一个TextBox控件,分别命名为pictureBox1、pictureBox2、textBox1。 2. 使用OpenCvSharp库加载您想要导入的图片,并在pictureBox1中显示。 ``` Mat image = Cv2.ImRead("your_image_path"); pictureBox1.Image = BitmapConverter.ToBitmap(image); ``` 3. 在窗体中添加一个按钮,命名为button1,点击该按钮时生成TextBox中的文字。 ``` private void button1_Click(object sender, EventArgs e) { string text = textBox1.Text; Font font = new Font("Arial", 16); Brush brush = Brushes.Red; PointF pointF = new PointF(10, 10); Bitmap bitmap = new Bitmap(pictureBox1.Image); Graphics graphics = Graphics.FromImage(bitmap); graphics.DrawString(text, font, brush, pointF); pictureBox2.Image = bitmap; } ``` 4. 在生成的Bitmap图像中,使用Graphics.DrawString方法绘制TextBox中的文字。 5. 将生成的新图像保存到全局变量中,并在pictureBox2中显示。 ``` private Bitmap newImage; private void button1_Click(object sender, EventArgs e) { string text = textBox1.Text; Font font = new Font("Arial", 16); Brush brush = Brushes.Red; PointF pointF = new PointF(10, 10); Bitmap bitmap = new Bitmap(pictureBox1.Image); Graphics graphics = Graphics.FromImage(bitmap); graphics.DrawString(text, font, brush, pointF); newImage = bitmap; pictureBox2.Image = newImage; } private void pictureBox2_MouseClick(object sender, MouseEventArgs e) { if (newImage != null) { pictureBox3.Image = newImage; } } ``` 通过以上步骤,您可以在OpenCvSharp窗体中导入一张图片,在TextBox中输入文字,在另一个PictureBox上生成TextBox中的文字的详细代码。希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值