110,577
社区成员
发帖
与我相关
我的任务
分享
//image对象初始化的时候创建的.
graphics = Graphics.FromImage(image);
//设置字体
Font font = new Font("黑体", 10, FontStyle.Bold);
//测试文字
string strTxt = "测试";
//在鼠标点击位置写字.
graphics.DrawString(strTxt, font, new SolidBrush(Color.Red), e.X, e.Y);
//释放
graphics.Dispose();
//显示写好字的图片
this.pictureBox1.Image = image;
Public Class Form1
Private point As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
point.X = e.X
point.Y = e.Y
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim font As New Font("黑体", 10, FontStyle.Bold)
Dim strTxt As String = "测试"
e.Graphics.DrawString(strTxt, font, New SolidBrush(Color.Red), point.X, point.Y)
End Sub
End Class
namespace WindowsApplication10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Bitmap Bmp = new Bitmap(pictureBox1.Image);
Graphics graphics = Graphics.FromImage(Bmp);
//设置字体
Font font = new Font("黑体", 10, FontStyle.Bold);
//测试文字
string strTxt = "测试";
//在鼠标点击位置写字.
if (pictureBox1.SizeMode == PictureBoxSizeMode.Normal ||
pictureBox1.SizeMode == PictureBoxSizeMode.AutoSize) // 按实际像素
graphics.DrawString(strTxt, font, new SolidBrush(Color.Red), e.Location);
else if (pictureBox1.SizeMode == PictureBoxSizeMode.StretchImage) // 按拉伸比例
{
graphics.DrawString(strTxt, font, new SolidBrush(Color.Red),
e.Location.X * pictureBox1.Image.PhysicalDimension.Width / pictureBox1.Width,
e.Location.Y * pictureBox1.Image.PhysicalDimension.Height / pictureBox1.Height);
}
else if (pictureBox1.SizeMode == PictureBoxSizeMode.Zoom)
{
// 没想好
}
else if (pictureBox1.SizeMode == PictureBoxSizeMode.CenterImage)
{
// 没想好
}
//释放
graphics.Dispose();
//显示写好字的图片
this.pictureBox1.Image = Bmp;
}
}
}
graphics = this.pictureBox1.CreateGraphics();
Font font = new Font("黑体", 10, FontStyle.Bold);
string strTxt = "测试";
graphics.DrawString(strTxt, font, new SolidBrush(Color.Red), e.X, e.Y);
graphics.Dispose();
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(SystemColors.Control);
g.DrawImage(this.pictureBox1.Image,0,0);
g.DrawString("test",this.Font,Brushes.Red,e.X,e.Y);
//this.pictureBox1_Paint(sender,new PaintEventArgs(g,this.pictureBox1.Bounds));
}