当前位置:网站首页>C# 给图片画 矩形,椭圆形,文字
C# 给图片画 矩形,椭圆形,文字
2022-07-16 21:49:00 【熊思宇】
代码
private void Button_Test_Click(object sender, EventArgs e)
{
string path = TextBox_Img1Path.Text;
if (string.IsNullOrEmpty(path))
{
Console.WriteLine("路径不能为空");
return;
}
if (!System.IO.File.Exists(path))
{
Console.WriteLine("图片不存在");
return;
}
Bitmap bitmap = new Bitmap(path);
Point point1 = new Point(100, 100);
Point point2 = new Point(120, 120);
//bitmap = DrawRoundInPicture(bitmap, point1, point2, Color.Red, 3, "起泡");
//pictureBox1.Image = bitmap;
Size size = new Size();
size.Width = 200;
size.Height = 200;
Bitmap bitmap1 = DrawRectangleInPicture(bitmap, point1, size, Color.Red, 2, "气泡", 13);
pictureBox1.Image = bitmap1;
//Point point3 = new Point(200, 200);
//Point point4 = new Point(210, 210);
//bitmap = DrawRoundInPicture(bitmap, point3, point4, Color.AntiqueWhite, 3, "破裂");
//bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
}
/// <summary>
/// 在图片上画椭圆
/// </summary>
/// <param name="bmp">图片的bitmap</param>
/// <param name="p0">位置</param>
/// <param name="lineColor">颜色</param>
/// <param name="lineWidth">圆圈厚度</param>
/// <param name="text">标题</param>
/// <param name="ds">线条的线型</param>
/// <returns></returns>
public static Bitmap DrawRoundInPicture(Bitmap bmp, Point p0, Point p1, Color lineColor, int lineWidth, string text, DashStyle ds = DashStyle.Solid)
{
if (bmp == null) return null;
Graphics g = Graphics.FromImage(bmp);
Brush brush = new SolidBrush(lineColor);
Pen pen = new Pen(brush, lineWidth);
pen.DashStyle = ds;
//画椭圆
//g.DrawEllipse(pen, new Rectangle(p0.X, p0.Y, Math.Abs(p0.X - p1.X), Math.Abs(p0.Y - p1.Y)));
//画矩形
g.DrawRectangle(pen, p0.X, p0.Y, 20, 20);
Font myFont = new Font("宋体", 30, FontStyle.Bold);
Brush bush = new SolidBrush(lineColor);//填充的颜色
g.DrawString(text, myFont, bush, p0.X - 30, p1.Y + 10);
g.Dispose();
return bmp;
}
/// <summary>
/// 图片上画矩形和标记文字
/// </summary>
/// <param name="bmp">图片bitmap</param>
/// <param name="pos">矩形的坐标位置</param>
/// <param name="size">矩形的宽高</param>
/// <param name="lineColor">线条的颜色</param>
/// <param name="lineWidth">线条</param>
/// <param name="text">矩形的文本</param>
/// <param name="fontSize">字体大小</param>
/// <param name="ds">线条的线型</param>
/// <returns></returns>
public static Bitmap DrawRectangleInPicture(Bitmap bmp, Point pos, Size size, Color lineColor, int lineWidth, string text, int fontSize, DashStyle ds = DashStyle.Solid)
{
if (bmp == null) return null;
Graphics g = Graphics.FromImage(bmp);
Brush brush = new SolidBrush(lineColor);
Pen pen = new Pen(brush, lineWidth);
pen.DashStyle = ds;
//画中心点(用于测试)
//g.DrawEllipse(pen, new Rectangle(pos.X, pos.Y, 3, 3));
//画矩形
int rectX = pos.X - (size.Width / 2);
int rectY = pos.Y - (size.Height / 2);
//g.DrawRectangle(pen, pos.X, pos.Y, size.Width, size.Height);
g.DrawRectangle(pen, rectX, rectY, size.Width, size.Height);
Font myFont = new Font("宋体", fontSize, FontStyle.Regular);
Brush bush = new SolidBrush(lineColor);//填充的颜色
//计算字体的长度
SizeF sizeF = g.MeasureString(text, myFont);
int fontPosX = (int)(pos.X - (sizeF.Width / 2));
int fontPosY = (int)(pos.Y + (sizeF.Height / 2) + (size.Height / 2));
Console.WriteLine();
g.DrawString(text, myFont, bush, fontPosX, fontPosY);
g.Dispose();
return bmp;
}
end
边栏推荐
猜你喜欢
随机推荐
验证两个字符串一致
leetcode-两数相加
Pay attention to using yyyy-mm-dd in the code!
[white box test] design method of logic coverage and path test
ACM板子
STM32F1与STM32CubeIDE编程实例-W25Q-SPI-Flash驱动
nodeJS中对Promise模块介绍
Leetcode sum of two numbers
Clean the disk with CMD command (mainly the system disk)
canal-deployer canal-adapter镜像构建,部署
动态内存管理(C语言)下--常见错误及大厂笔试题
Over fitting and under fitting
最大子数组异或和
p5.js向上生长js特效
Expérience de l'arbre binaire
Pytest interface automated testing framework | introduction to pytest
剑指 Offer II 119. 最长连续序列
为什么 Nodejs 这么快?
Recent software test interview questions encountered by group Friends
Canadian deployer Canadian adapter image construction, deployment








