/// <summary>
/// 按指定宽高缩放图片
/// </summary>
/// <param name="image">原图片</param>
/// <param name="dstWidth">目标图片宽</param>
/// <param name="dstHeight">目标图片高</param>
/// <returns></returns>
public Image ScaleImage(Image image, int dstWidth, int dstHeight)
{
Graphics g = null;
try
{
//按比例缩放
float scaleRate = GetWidthAndHeight(image.Width, image.Height, dstWidth, dstHeight);
int width = (int)(image.Width * scaleRate);
int height = (int)(image.Height * scaleRate); Bitmap destBitmap = new Bitmap(width, height);
g = Graphics.FromImage(destBitmap);
g.Clear(Color.Transparent); //设置画布的描绘质量
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); return destBitmap;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
finally
{
if (g != null)
{
g.Dispose();
} if (image != null)
{
image.Dispose();
}
}
} /// <summary>
/// 获取图片缩放比例
/// </summary>
/// <param name="oldWidth">原图片宽</param>
/// <param name="oldHeigt">原图片高</param>
/// <param name="newWidth">目标图片宽</param>
/// <param name="newHeight">目标图片高</param>
/// <returns></returns>
public float GetWidthAndHeight(int oldWidth, int oldHeigt, int newWidth, int newHeight)
{
//按比例缩放
float scaleRate;
if (oldWidth >= newWidth && oldHeigt >= newHeight)
{
int widthDis = oldWidth - newWidth;
int heightDis = oldHeigt - newHeight;
if (widthDis > heightDis)
{
scaleRate = newWidth * 1f / oldWidth;
}
else
{
scaleRate = newHeight * 1f / oldHeigt;
}
}
else if (oldWidth >= newWidth)
{
scaleRate = newWidth * 1f / oldWidth;
}
else if (oldHeigt >= newHeight)
{
scaleRate = newHeight * 1f / oldHeigt;
}
else
{
int widthDis = newWidth - oldWidth;
int heightDis = newHeight - oldHeigt;
if (widthDis > heightDis)
{
scaleRate = newHeight * 1f / oldHeigt;
}
else
{
scaleRate = newWidth * 1f / oldWidth;
}
}
return scaleRate;
}

C# 按指定宽高缩放图片的更多相关文章

  1. Android大图片之缩略图,以及对原图依照指定宽高裁剪成缩略图

     <Android大图片之变换缩略图,以及对原始大图片依照指定宽.高裁剪成缩略图> 在Android的ImageView载入图像资源过程中,出于性能和内存开销的须要.有时候须要把一个原 ...

  2. php 图片上传的公共方法(按图片宽高缩放或原图)

    写的用于图片上传的公共方法类调用方法: $upload_name='pic';$type = 'logo_val';$file_name = 'logo_' . $user_id .create_st ...

  3. ThinkPHP 5 文件上传及指定宽高生成缩略图公共方法

    这个是非常常用的案例,ThinkPHP 5 文件上传及指定宽高生成缩略图公共方法/** * 单文件上传 * name:表单上传文件的名字 * ext: 文件允许的后缀,字符串形式 * path:文件保 ...

  4. Matrix: 利用Matrix来设置ImageView的宽高,使图片能正常显示

    在Android中加载ImageView一般都不会给ImageView的宽高设置一个确切的值,一般都是直接写成: <ImageView android:id="@+id/iv_test ...

  5. 头像修改功能 包含ios旋转图片 但是旋转后没遮罩, 正常图片可以显示遮罩 宽高不规则图片没做控制 遮罩框可以拖动

    https://blog.csdn.net/wk767113154/article/details/77989544  参考资料 <template> <div id="p ...

  6. js 获取图片宽高 和 图片大小

    获取要查看大小的img var img_url = 'http://img5.imgtn.bdimg.com/it/u=4267222417,1017407570&fm=200&gp= ...

  7. excel表格添加固定宽高的图片

    import xlsxwriter,xlrd import glob #打开excel文件 data=xlrd.open_workbook('优秀创意库-20180725.xlsx') #获取第一张工 ...

  8. JS实现图片宽高的等比缩放

    关于图片宽高的等比缩放,其实需求就是让图片自适应父容器的宽高,并且是等比缩放图片,使图片不变形. 例如,需要实现如下的效果: 要实现上面的效果,需要知道图片的宽高,父容器的宽高,然后计算缩放后的宽高. ...

  9. java高质量缩放图片

    可按照比例缩放,也可以指定宽高 import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg.JP ...

  10. table-cell实现未知宽高图片,文本水平垂直居中在div

    <BODY> <h1>未知宽高的图片水平垂直居中在div</h1> <!--box-outer--> <div class="box-o ...

随机推荐

  1. Linux 中iostat 命令详解

    iostat命令详解 iostat 主要是统计 磁盘活动情况. iostat有以下缺陷: iostat的输出结果大多数是一段时间内的平均值,因此难以反映峰值情况iostat仅能对系统整体情况进行分析汇 ...

  2. JavaScript选择器

    Js选择器 JS选择器常用的有getElementById().getElementsByClassName().getElementsByName().getElementsByTagName(). ...

  3. c# 代码操作ftp服务器文件

    好久不见,我又回来了.给大家分享一个最近c#代码操作ftp服务器的代码示例 1 public abstract class FtpOperation 2 { 3 /// <summary> ...

  4. python课本学习-第一章

    chapter 1 python开发入门 1.python之父:Guido van Rossum 2.python语言的特征: 简单 易学 免费&开源 可移植性 解释性 面向对象 在面向对象的 ...

  5. Innodb存储引擎之锁

    目录 一.概述 二.lock 与 latch 三.Innodb存储引擎中的锁 锁 一致性非锁定读 一致性锁定读 自增长与锁 外键与锁 四.锁的算法 锁的算法 Phantom Problem 幻读问题 ...

  6. 关于char * 和 char [] 的一点理解

    截取一段有用的信息: c++的char[]和char*的区别 char str1[] = "abc": 这里的"abc"是一个常量,首先会在常量存储区里存储&q ...

  7. python字典操作的大O效率

  8. Advanced .Net Debugging 2:CLR基础

    一.简介 这是2024新年后我的第一篇文章,也是我的<Advanced .Net Debugging>这个系列的第二篇文章.这篇文章告诉我们为了进行有效的程序调试,我们需要掌握哪些知识.言 ...

  9. 从全球顶级数据库大会 SIGMOD 看数据库发展趋势

    本文来自 NebulaGraph 的软件工程师文豪在美国费城参加 2022 年 SIGMOD 大会时的见闻.SIGMOD 是数据库领域的顶级会议之一,是 CCF 数据库 / 数据挖掘 / 内容检索领域 ...

  10. windows编译ZLMediaKit流媒体服务webrtc

    环境说明 ZLMediaKit编译需要的软件 visual studio 2022 cmake 3.29.0-rc2 OpenSSL 1.1.1w(不想踩坑的话安装这个版本) libsrtp 2.6. ...