C#剪切生成高质量缩放图片
/// <summary>
/// 高质量缩放图片
/// </summary>
/// <param name="OriginFilePath">源图的路径</param>
/// <param name="TargetFilePath">存储缩略图的路径</param>
/// <param name="DestWidth">缩放后图片宽度</param>
/// <param name="DestHeight">缩放后图片高度</param>
/// <returns>表明此次操作是否成功</returns>
public static bool ToSuolue(System.IO.Stream oStream, string TargetFilePath, int towidth, int toheight)
{
//System.Drawing.Image OriginImage = System.Drawing.Image.FromFile(OriginFilePath);
System.Drawing.Image OriginImage = System.Drawing.Image.FromStream(oStream);
System.Drawing.Imaging.ImageFormat thisFormat = OriginImage.RawFormat;
//按比例缩放
int x = , y = ;
int ow = OriginImage.Width;
int oh = OriginImage.Height; if ((double)ow / (double)oh > (double)towidth / (double)toheight)
{
oh = OriginImage.Height;
ow = OriginImage.Height * towidth / toheight;
y = ;
x = (OriginImage.Width - ow) / ;
}
else
{
ow = OriginImage.Width;
oh = OriginImage.Width * toheight / towidth;
x = ;
y = (OriginImage.Height - oh) / ;
} Bitmap bt = new Bitmap(towidth, toheight); //根据指定大小创建Bitmap实例
using (Graphics g = Graphics.FromImage(bt))
{
g.Clear(Color.White);
//设置画布的描绘质量
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(OriginImage, new Rectangle(, , towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
} System.Drawing.Imaging.EncoderParameters EncoderParams = new System.Drawing.Imaging.EncoderParameters(); //取得内置的编码器
long[] Quality = new long[];
Quality[] = ;
System.Drawing.Imaging.EncoderParameter EncoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Quality);
EncoderParams.Param[] = EncoderParam; //try
//{
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
for (int i = ; i < arrayICI.Length; i++)
{
if (arrayICI[i].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[i]; //设置为JPEG编码方式
break;
}
} if (jpegICI != null) //保存缩略图
{
bt.Save(TargetFilePath, jpegICI, EncoderParams);
}
else
{
bt.Save(TargetFilePath, thisFormat);
}
OriginImage.Dispose();
return true;
//}
//catch (System.Runtime.InteropServices.ExternalException e1) //GDI+发生一般错误
//{
// return false;
//}
//catch (Exception e2)
//{
// return false;
//}
}
C#剪切生成高质量缩放图片的更多相关文章
- java高质量缩放图片
可按照比例缩放,也可以指定宽高 import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg.JP ...
- C#放缩、截取、合并图片并生成高质量新图的类
原文:C#放缩.截取.合并图片并生成高质量新图的类 using System;using System.Drawing;using System.Drawing.Imaging;using Syste ...
- atitit.thumb生成高质量缩略图 php .net c++ java
atitit.java thumb生成高质量缩略图 php .net c++ 1. 图像缩放(image scaling)---平滑度(smoothness)和清晰度(sharpness) 1 2. ...
- 移动Web—CSS为Retina屏幕替换更高质量的图片
来源:互联网 作者:佚名 时间:12-24 10:37:45 [大 中 小] 点评:Retian似乎是屏幕显示的一种趋势,这也是Web设计师面对的一个新挑战;移动应用程序的设计师们已经学会了如何为Re ...
- 使用 FFmpeg 处理高质量 GIF 图片
使用 FFmpeg 处理高质量 GIF 图片 - 为程序员服务 http://ju.outofmemory.cn/entry/169845
- C# GDI生成清晰【高质量】图片
对于GDI+,在正常的操作,Bitmap,Graphcis,DrawImage或者DrawString ,生成图片的话,会产生很多杂点,或者是图片质量不稳定.尤其是在读取图片后,生成缩略图之后,文件会 ...
- Python高质量缩放切图,抗锯齿
最近刚接触Python,以迅雷不及掩耳盗铃之势(只是迫不及待)应用到工作中去了之前用 cmd+photoshop做批量图像处理(缩放切片),在执行效率(速度)上和灵活度上有很大限制,遂转战Python ...
- Highcharts结合PhantomJS在服务端生成高质量的图表图片
项目背景 最近忙着给部门开发一套交互式的报表系统,来替换原有的静态报表系统. 老系统是基于dotnetCHARTING开发的,dotnetCHARTING的优势是图表类型丰富,接口调用简单,使用时只需 ...
- java图片高质量缩放类
import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.awt.image.BufferedI ...
随机推荐
- 4.SpringMVC 配置式开发-处理器映射器
处理器映射器 HandlerMapping HandlerMapping 接口负责根据request请求找到对应的Handler处理器及Interceptor拦截器, 并将它们封装在HandlerEx ...
- What is libacl.so.1 ?
Google says, "This package contains the libacl.so dynamic library which contains the POSIX 1003 ...
- Delphi 线程的基本概念
- 网络编程基础之TCP学习(二)编程案例
TCP网络编程流程如下: 实现功能:服务器端与客户端成功通讯后返回get! 服务器端程序 #include <netdb.h> #include <sys/socket.h> ...
- printf固定一行打印倒计时的实现
@2019-07-15 [小记] #include<stdlib.h> #include <stdio.h> #include <time.h> #include ...
- 04_Hive的基本命令
1.Hive基本操作: 1.1.Hive的建表语句: CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [ ...
- python __file__ is not defined 解决方法
python __file__ is not defined 解决方法 __file__ 是在python module 被导入的时候生成的一个变量,所以在 __file__ 不能被使用,但是又想获取 ...
- Solving Docker permission denied while trying to connect to the Docker daemon socket
The error message tells you that your current user can’t access the docker engine, because you’re la ...
- 解读>/dev/null 2>&1
背景 我们经常能在shell脚本中发现>/dev/null 2>&1这样的语句.以前的我并没有去深入地理解这段命令的作用,照搬照用,今天开始去解读>/dev/null 2&g ...
- qt 启动参数 -qws
运行嵌入式程序 在嵌入式QT版本中,程序需要服务器或自己作为服务器程序. 服务器程序构造的方法是构造一个QApplication::GuiServe类型的QApplication对象.或者使用-qws ...