C#图片按比例缩放建议收藏

C#图片按比例缩放:1//按比例缩放图片2publicImageZoomPicture(ImageSourceImage,intTargetWidth,intTargetHei

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

全栈程序员社区此处内容已经被作者隐藏,请输入验证码查看内容
验证码:
请关注本站微信公众号,回复“验证码”,获取验证码。在微信里搜索“全栈程序员社区”或者“www_javaforall_cn”或者微信扫描右侧二维码都可以关注本站微信公众号。

C#图片按比例缩放:

 1       // 按比例缩放图片
 2         public Image ZoomPicture(Image SourceImage, int TargetWidth, int TargetHeight)
 3         {
 4             int IntWidth; //新的图片宽
 5             int IntHeight; //新的图片高
 6             try
 7             {
 8                 System.Drawing.Imaging.ImageFormat format = SourceImage.RawFormat;
 9                 System.Drawing.Bitmap SaveImage = new System.Drawing.Bitmap(TargetWidth, TargetHeight);
10                 Graphics g = Graphics.FromImage(SaveImage);
11                 g.Clear(Color.White);
12 
13                 //计算缩放图片的大小 http://www.cnblogs.com/roucheng/
14 
15                 if (SourceImage.Width > TargetWidth && SourceImage.Height <= TargetHeight)//宽度比目的图片宽度大,长度比目的图片长度小
16                 {
17                     IntWidth = TargetWidth;
18                     IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
19                 }
20                 else if (SourceImage.Width <= TargetWidth && SourceImage.Height > TargetHeight)//宽度比目的图片宽度小,长度比目的图片长度大
21                 {
22                     IntHeight = TargetHeight;
23                     IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
24                 }
25                 else if (SourceImage.Width <= TargetWidth && SourceImage.Height <= TargetHeight) //长宽比目的图片长宽都小
26                 {
27                     IntHeight = SourceImage.Width;
28                     IntWidth = SourceImage.Height;
29                 }
30                 else//长宽比目的图片的长宽都大
31                 {
32                     IntWidth = TargetWidth;
33                     IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
34                     if (IntHeight > TargetHeight)//重新计算
35                     {
36                         IntHeight = TargetHeight;
37                         IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
38                     }
39                 }
40 
41                 g.DrawImage(SourceImage, (TargetWidth - IntWidth) / 2, (TargetHeight - IntHeight) / 2, IntWidth, IntHeight);
42                 SourceImage.Dispose();
43 
44                 return SaveImage;
45             }
46             catch (Exception ex)
47             {
48               
49             }
50 
51             return null;
52         }

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120740.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • golang激活码破解【最新永久激活】

    (golang激活码破解)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~9AAG1RZ8NI-eyJsaWNlbnNlSWQiOi…

    2022年4月1日
    697
  • java-version未找到命令_org.apache.catalina.core.stand

    java-version未找到命令_org.apache.catalina.core.standspringboot2.x工程,将jasypt升级到3.x–>3.0.3后,配置一个属性为加密,工程启动后报错如下############################################################

    2022年9月26日
    3
  • webpack开发环境和生产环境_环境问题是如何产生和发展的

    webpack开发环境和生产环境_环境问题是如何产生和发展的前言如果我们需要使用webpack,就需要依赖node环境nvmnodenpmwebpack@cliwebpacknvm安装nvm是一个用来管理node版本的工具。我们之所以需要使用n

    2022年7月30日
    5
  • python resize函数怎么用_Python numpy.resize函数方法的使用

    numpy.resizenumpy.resize(a,new_shape)[source]返回具有指定形状的新数组。如果新数组大于原始数组,则新数组将填充a的重复副本。请注意,此行为与a.resize(new_shape)不同,后者用零而不是重复的a填充。参数:a:array_like要调整大小的数组。new_shape:int或int类型的tuple调整大小后的数组的…

    2022年4月7日
    400
  • 和黑客斗争的 6 天![通俗易懂]

    和黑客斗争的 6 天![通俗易懂]互联网公司工作,很难避免不和黑客们打交道,我呆过的两家互联网公司,几乎每月每天每分钟都有黑客在公司网站上扫描。有的是寻找Sql注入的缺口,有的是寻找线上服务器可能存在的漏洞,大部分都…

    2022年6月11日
    31
  • 游标和动态SQL

    游标和动态SQL游标类别:静态游标(指在编译的时候,游标就与一个select语句进行了静态绑定的游标,这种游标只能作用于一个查询语句)和动态游标(就是希望我们的查询语句在运行的时候才跟游标绑定,为了使用动态游标,必须声明游标变量)。动态游标分两种,分别是强类型和弱类型。强类型的动态游标只能支持查询结果与他类型匹配的这种查询语句,弱类型的动态游标可以支持任何的查询语句。静态游标分为两种,隐式游标和显

    2022年6月23日
    28

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号