1. 程式人生 > 其它 >C# 重繪圖片.圖片加字,加矩形,加圓,加線,根據XY座標修改RGB

C# 重繪圖片.圖片加字,加矩形,加圓,加線,根據XY座標修改RGB

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Net.Http;

namespace SiteApp.BLL.Common
{
    public class ImageHelper
    {
        /// <summary>
        /// 獲取網路路勁的圖片
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
public static Image GetHttpImage(string url) { var client = new HttpClient(); var uri = new Uri(Uri.EscapeUriString(url)); byte[] urlContents = client.GetByteArrayAsync(uri).Result; using (var ms = new System.IO.MemoryStream(urlContents)) {
return Image.FromStream(ms); } } /// <summary> /// 將Base64字串轉換為Image物件 /// </summary> /// <param name="base64Str">base64字串</param> /// <returns></returns> public static Bitmap Base64StrToImage(string base64Str) { Bitmap bitmap
= null; try { byte[] arr = Convert.FromBase64String(base64Str); MemoryStream ms = new MemoryStream(arr); Bitmap bmp = new Bitmap(ms); ms.Close(); bitmap = bmp; } catch (Exception ex) { } return bitmap; } /// <summary> /// 圖片加字 /// </summary> /// <param name="old"></param> /// <param name="text"></param> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public static Bitmap ImgDrawString(Bitmap old,string text,int x,int y) { #region 圖片加字 Bitmap bmp = new Bitmap(old); Graphics g = Graphics.FromImage(bmp); Font font = new Font("宋體", 15); SolidBrush sbrush = new SolidBrush(Color.Black); g.DrawString(text, font, sbrush, new PointF(x, y)); MemoryStream ms = new MemoryStream(); #endregion return bmp; } /// <summary> /// 圖片加矩形 /// </summary> /// <param name="old"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Bitmap ImgDrawRectangle(Bitmap old,int x,int y,int width,int height) { Bitmap bmp = new Bitmap(old); #region 矩形 Graphics g = Graphics.FromImage(bmp); Brush brush = new SolidBrush(Color.Black); Pen pen = new Pen(brush, 1); pen.DashStyle = DashStyle.DashDot; g.DrawRectangle(pen, new Rectangle(x, y, width, height)); g.Dispose(); #endregion return bmp; } /// <summary> /// 圖片加圓 /// </summary> /// <param name="old"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Bitmap ImgDrawEllipse(Bitmap old, int x, int y, int width, int height) { Graphics g = Graphics.FromImage(old); Brush brush = new SolidBrush(Color.Black); Pen pen = new Pen(brush, 1); pen.DashStyle = DashStyle.DashDot; g.DrawEllipse(pen, new Rectangle(x, y, width, height)); g.Dispose(); return old; } /// <summary> /// 圖片加線 /// </summary> /// <param name="old"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="x1"></param> /// <param name="y1"></param> /// <returns></returns> public static Bitmap ImaDrawLine(Bitmap old, int x, int y, int x1, int y1) { Graphics g = Graphics.FromImage(old); Brush brush = new SolidBrush(Color.Black); Pen pen = new Pen(brush, 1); pen.DashStyle = DashStyle.DashDot; g.DrawLine(pen, x, y, x1, y1); g.Dispose(); return old; } /// <summary> /// 圖片根據XY修改RGB /// </summary> /// <param name="old"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="r"></param> /// <param name="g"></param> /// <param name="b"></param> /// <returns></returns> public static Bitmap ImaDrawPixel(Bitmap old, int x, int y, int r, int g, int b) { var rgb = old.GetPixel(x, y); old.SetPixel(x, y, Color.FromArgb(r, g, b)); return old; } } }

呼叫方式

Bitmap image = (Bitmap)ImageHelper.GetHttpImage(newInfraredList[i].ImgBase64);// 這裡是HTTP:// 開頭的圖片路勁

var img = ImageHelper.Base64StrToImage(entity.ImgBase64);


var newImg = ImageHelper.ImgDrawString(newImg, $"最高溫度:{ list[b].Max}°C,最低溫度:{ list[b].Min}°C", x - 10, y - 10);

var newImg = ImageHelper.ImgDrawRectangle(image, x, y, width, height);

var newImg = ImageHelper.ImgDrawEllipse(image, x, y, width, height);

var newImg = ImageHelper.ImaDrawLine(image, x, y, x1, y1);

var bmpTemp = ImageHelper.ImaDrawPixel(bmp,0,0,255,255,255); 

最後圖片重繪的效果如下: