C#编程,图片拼合、拼贴、合并
2022/3/19 17:29:19
本文主要是介绍C#编程,图片拼合、拼贴、合并,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace ConsoleApp5 { class Program { static void Main(string[] args) { CombinImage(); } /// <summary> /// 方法一 /// 多张图片的合并 /// </summary> static private void CombinImage() { const string folder = @"F:\测试图片"; Image img1 = Image.FromFile(Path.Combine(folder, "测试1.png")); Bitmap map1 = new Bitmap(img1); Image img2 = Image.FromFile(Path.Combine(folder, "测试2.png")); Bitmap map2 = new Bitmap(img2); Image img3 = Image.FromFile(Path.Combine(folder, "测试3.png")); Bitmap map3 = new Bitmap(img3); var width = Math.Max(img1.Width, img2.Width); var height = img1.Height + img2.Height + 10; // 初始化画布(最终的拼图画布)并设置宽高 Bitmap bitMap = new Bitmap(width, height); // 初始化画板 Graphics g1 = Graphics.FromImage(bitMap); // 将画布涂为白色(底部颜色可自行设置) g1.FillRectangle(Brushes.White, new Rectangle(0, 0, width, height)); //在x=0,y=0处画上图一 g1.DrawImage(map1, 0, 0, img1.Width, img1.Height); //在x=0,y在图一往下10像素处画上图二 g1.DrawImage(map2, 0, img1.Height + 10, img2.Width, img2.Height); g1.DrawImage(map3, 0, img1.Height + 10, img3.Width, img3.Height); map1.Dispose(); map2.Dispose(); map3.Dispose(); Image img = bitMap; //保存 img.Save(Path.Combine(folder, "new测试1.png")); img.Dispose(); } /// <summary> /// 方法二 ///实现左右拼接图片 /// </summary> /// <param name="Img1"></param> /// <param name="Img2"></param> static private void CombinImage2(Image Img1, Image Img2) { #if FALSE //控制台调用 const string folder = @"F:\测试图片"; Image img1 = Image.FromFile(Path.Combine(folder, "测试1.png")); Image img2 = Image.FromFile(Path.Combine(folder, "测试2.png")); JoinImage(img1, img2); #endif int imgHeight = 0, imgWidth = 0; imgWidth = Img1.Width + Img2.Width; imgHeight = Math.Max(Img1.Height, Img2.Height); Bitmap joinedBitmap = new Bitmap(imgWidth, imgHeight); Graphics graph = Graphics.FromImage(joinedBitmap); graph.DrawImage(Img1, 0, 0, Img1.Width, Img1.Height); graph.DrawImage(Img2, Img1.Width, 0, Img2.Width, Img2.Height); Image img = joinedBitmap; //保存 const string folder = @"F:\测试图片"; img.Save(Path.Combine(folder, "new测试2.png")); img.Dispose(); } /// <summary> /// 方法三 /// 调用此函数后使此两种图片合并,类似相册,有个 /// 背景图,中间贴自己的目标图片 /// </summary> /// <param name="sourceImg">粘贴的源图片</param> /// <param name="destImg">粘贴的目标图片</param> static private void CombinImage3() { const string folder = @"F:\测试图片"; Image img1 = Image.FromFile(Path.Combine(folder, "测试1.png"));//相框图片 Image img2 = Image.FromFile(Path.Combine(folder, "测试2.png")); //照片图片 //从指定的System.Drawing.Image创建新的System.Drawing.Graphics Graphics g = Graphics.FromImage(img1); g.DrawImage(img1, 0, 0, 148, 124);// g.DrawImage(imgBack, 0, 0, 相框宽, 相框高); //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框 //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高); g.DrawImage(img2, 17, 17, 112, 73); GC.Collect(); img1.Save(Path.Combine(folder, "new测试3.png")); img1.Dispose(); } } }
原文链接:https://www.cnblogs.com/macT/p/11192200.html
这篇关于C#编程,图片拼合、拼贴、合并的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具