C#对图片进行缩放变换
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Windows.Forms; namespace ScaleImage { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Image s = Image.FromFile("D:\\image\\22.jpg"); Image bgImage = ZoomPicture(s, 0.5f, 0.5f); //M,N大于1,为放大图片,小于1为缩小图片 panel1.Width = bgImage.Width; //设置画板宽度为图片宽度 panel1.Height = bgImage.Height; //设置画板高度为图片高度 panel1.BackgroundImage = bgImage; BitMap bmap = new BitMap(bgImage); bmap.Save("D:\\image\\23.jpg", ImageFormt.Jpeg); bmap.Dispose(); } // 按比例缩放图片 public Image ZoomPicture(Image SourceImage, float M, float N) { int IntWidth; //新的图片宽 int IntHeight; //新的图片高 int TargetWidth = (int)(M * SourceImage.Width); //取整,是因为下面的Bitmap的两个参数只能为整数 int TargetHeight = (int)(N * SourceImage.Height); try { ImageFormat format = SourceImage.RawFormat; Bitmap SaveImage = new Bitmap(TargetWidth, TargetHeight); Graphics g = Graphics.FromImage(SaveImage); g.Clear(Color.White); //计算缩放图片的大小 IntHeight = TargetHeight; IntWidth = TargetWidth; g.DrawImage(SourceImage, 0, 0, IntWidth, IntHeight); //在指定坐标处画指定大小的图片 SourceImage.Dispose(); return SaveImage; } catch (Exception ex) { } return null; } } }
https://www.freesion.com/article/72051025683/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2016-07-08 AuthenticationManager.SignOut() 无法注销用户的问题