当前位置:网站首页>C # draw rectangle, ellipse and text for the picture
C # draw rectangle, ellipse and text for the picture
2022-07-19 00:10:00 【Xiongsiyu】
Code
private void Button_Test_Click(object sender, EventArgs e)
{
string path = TextBox_Img1Path.Text;
if (string.IsNullOrEmpty(path))
{
Console.WriteLine(" The path cannot be empty ");
return;
}
if (!System.IO.File.Exists(path))
{
Console.WriteLine(" Picture does not exist ");
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, " Blistering ");
//pictureBox1.Image = bitmap;
Size size = new Size();
size.Width = 200;
size.Height = 200;
Bitmap bitmap1 = DrawRectangleInPicture(bitmap, point1, size, Color.Red, 2, " Bubble ", 13);
pictureBox1.Image = bitmap1;
//Point point3 = new Point(200, 200);
//Point point4 = new Point(210, 210);
//bitmap = DrawRoundInPicture(bitmap, point3, point4, Color.AntiqueWhite, 3, " rupture ");
//bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
}
/// <summary>
/// Draw an ellipse on the picture
/// </summary>
/// <param name="bmp"> The image bitmap</param>
/// <param name="p0"> Location </param>
/// <param name="lineColor"> Color </param>
/// <param name="lineWidth"> Circle thickness </param>
/// <param name="text"> title </param>
/// <param name="ds"> Line type </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;
// Drawing ellipse
//g.DrawEllipse(pen, new Rectangle(p0.X, p0.Y, Math.Abs(p0.X - p1.X), Math.Abs(p0.Y - p1.Y)));
// Draw a rectangular
g.DrawRectangle(pen, p0.X, p0.Y, 20, 20);
Font myFont = new Font(" Song style ", 30, FontStyle.Bold);
Brush bush = new SolidBrush(lineColor);// Fill color
g.DrawString(text, myFont, bush, p0.X - 30, p1.Y + 10);
g.Dispose();
return bmp;
}
/// <summary>
/// Draw a rectangle and mark text on the picture
/// </summary>
/// <param name="bmp"> picture bitmap</param>
/// <param name="pos"> Coordinate position of rectangle </param>
/// <param name="size"> The width and height of the rectangle </param>
/// <param name="lineColor"> The color of the line </param>
/// <param name="lineWidth"> line </param>
/// <param name="text"> Rectangular text </param>
/// <param name="fontSize"> font size </param>
/// <param name="ds"> Line type </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;
// Draw the center point ( Used for testing )
//g.DrawEllipse(pen, new Rectangle(pos.X, pos.Y, 3, 3));
// Draw a rectangular
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(" Song style ", fontSize, FontStyle.Regular);
Brush bush = new SolidBrush(lineColor);// Fill color
// Calculate the length of the font
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
边栏推荐
- php通过form表单上传Excel文件并把Excel数据导入到数据库中
- Introduction to the universal theme system of SAP appgyver
- 20220707 线程学习 继承
- 应用的无状态设计
- 程序中使用protobuf
- About the use of go modules environment construction and package management tools
- matplotlib. Pyplot use (subplots, gray
- How does electron package its Kirin system to support godson, fangteng and megacore platforms
- Account creation + login + contact form code
- How to use redis to realize distributed cache
猜你喜欢

Kept - solve the problem that the primary and standby nodes seize the VIP at the same time

Vmware6.0 connection Qunhui iSCSI

Responsive form style transparent design

liunx中FileDescriptor与打开文件之间的关系

Redis distributed cache - Data Persistence

Glide source code analysis (4.13.2)

Comment gérer les erreurs courantes lors de l'installation de MySQL

Redis三种模式——主从复制、哨兵模式、集群

Mise en œuvre du modèle word2vec skip Gram

WordPress主题分享:Avada主题v7.8.0免费下载 2022年最新版
随机推荐
MySQL transaction isolation mechanism
shell条件判断练习
Find all letter ectopic words in the string
[examination] answer to the examination of class 2201 in stage II (no responsibility for mistakes)
go+mysql+redis+vue3简单聊室,第5弹:使用消息队列和定时任务同步消息到mysql
WordPress Theme sharing: the7 theme v10.11 download the latest version of 2022 for free
[动态规划]DP24 打家劫舍(二)-中等
fiddler抓不到PC端微信小程序的包
OpenCV DFT
matplotlib. Pyplot use (subplots, gray
Account creation + login + contact form code
Envoy lifecycle management
Maximum subarray XOR and
[acwing weekly rematch] game 60 20220716
Openpose: estimation de la pose 2D Multi - personnes en temps réel à l'aide d'un champ d'affinité partiel
TCP 糊涂窗口综合症(silly window syndrome)与 rate-based 流控
Glide source code analysis (4.13.2)
A set of simple multipurpose form widget code
【JS】webAPI---第一弹
乐观锁和悲观锁在kubernetes中的应用