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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • flume什么意思_FlumenStellarum

    flume什么意思_FlumenStellarum1.HdfsSinka1.channels=c1a1.sinks=k1a1.sinks.k1.type=hdfsa1.sinks.k1.channel=c1a1.sinks.k1.hdfs.path=/flume/events/%y-%m-%d/%H%M/%Sa1.sinks.k1.hdfs.filePrefix=events-a1.sinks.k1.

    2025年6月5日
    2
  • 年龄大的普通程序员最后的出路是哪里?[通俗易懂]

    年龄大的普通程序员最后的出路是哪里?

    2022年2月11日
    64
  • rsyslog丢数据_linux日志自动丢失

    rsyslog丢数据_linux日志自动丢失最近发现跑keepalived的几台机器的日志总是打印不完,还好给抛了一个报错,信息如下:[root@yw_lvs2_backupetc]#tail-n1000000/var/log/messages-20130526|grep”rate-limiting”May2011:43:55yw_lvs2_backuprsyslogd-2177:imuxsockbe

    2022年8月15日
    6
  • RStudio安装Rtools[通俗易懂]

    RStudio安装Rtools[通俗易懂]Rtools不能通过常规install.packages()命令进行安装,需要通过installr包进行安装install.packages(“installr”)install.packages(“stringr”)###依赖包library(stringr)library(installr)注:其他的GUI(SuchAs:R,’Rtools’,’RStudio’…

    2022年6月27日
    132
  • mysql一主多从 读写分离_mysql读写分离原理

    mysql一主多从 读写分离_mysql读写分离原理简介:什么是主从复制,如何实现读写分离,看这篇你就懂了!思维导图文章已收录到我的Github精选,欢迎Star:https://github.com/yehongzhi/learningSummary前言在很多项目,特别是互联网项目,在使用MySQL时都会采用主从复制、读写分离的架构。为什么要采用主从复制读写分离的架构?如何实现?有什么缺点?让我们带着这些问题开始这段学习之旅吧!为什么使用主从复制、读写分离主从复制、读写分离一般是一起使用的。目的很简单,就是为了提高数据库的并发性能。你想,假设是单机,读

    2022年8月13日
    7
  • linux fork函数浅析

    linux fork函数浅析

    2021年12月5日
    73

发表回复

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

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