C# 图片剪切与缩小的实例
时间:2021-12-31 14:32:53
public void CutToF(Stream stream)
{
Image initImage = Image.FromStream(stream, true);
if (initImage.Width <= 480 && initImage.Height <= 480)
{
initImage.Save("D://test.jpg", ImageFormat.Jpeg);
}
else
{
int initWidth = initImage.Width;
int initHeight = initImage.Height;
if (initWidth != initHeight)
{
Image pickedImage = null;
Graphics pickedG = null;
if (initWidth > initHeight)
{
pickedImage = new Bitmap(initHeight, initHeight);
pickedG = Graphics.FromImage(pickedImage);
pickedG.InterpolationMode = InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = SmoothingMode.HighQuality;
Rectangle fromR = new Rectangle(0, 0, initHeight, initHeight);//(initWidth - initHeight) / 2
Rectangle toR = new Rectangle(0, 0, initHeight, initHeight);
pickedG.DrawImage(initImage, fromR);
pickedImage.Save("D://jpg//test2.jpg");
initWidth = initHeight;
}
initImage = (Image)pickedImage.Clone();
pickedG.Dispose();
pickedImage.Dispose();
initImage.Save("D://jpg//test1.jpg");
}
Image resultImage = new Bitmap(150, 150);
Graphics resultG = Graphics.FromImage(resultImage);
resultG.InterpolationMode = InterpolationMode.HighQualityBicubic;
resultG.SmoothingMode = SmoothingMode.HighQuality;
resultG.Clear(Color.White);
resultG.DrawImage(initImage, new Rectangle(0, 0, 100, 100), new Rectangle(0, 0, initWidth, initHeight), GraphicsUnit.Pixel);
ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo item in icis)
{
if (item.MimeType == "image/jpeg" || item.MimeType == "image/bmp" || item.MimeType == "image/png" || item.MimeType =="image/gif")
{
ici = item;
}
}
EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
resultImage.Save("D://jpg//test.jpg", ici, ep);
ep.Dispose();
resultG.Dispose();
resultImage.Dispose();
initImage.Dispose();
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring maven filtering使用方法详解
Java构建JDBC应用程序的实例操作
Android之使用Bundle进行IPC详解
Mybatis中 mapper-locations和@MapperScan的作用
![](https://img.aspxhome.com/file/2023/0/57700_0s.png)
深度理解Java访问修饰符
![](https://img.aspxhome.com/file/2023/8/76598_0s.png)
Java多个版本切换的几种方法
![](https://img.aspxhome.com/file/2023/8/63108_0s.png)
Java设计模式之命令模式_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/3/69913_0s.png)
解决IDEA中maven导入jar包一直报错问题
![](https://img.aspxhome.com/file/2023/4/72414_0s.png)
Java 中的 xx ≠ null 是什么新语法
![](https://img.aspxhome.com/file/2023/1/78901_0s.png)
Android Studio用genymotion运行后小图标无法显示问题
![](https://img.aspxhome.com/file/2023/7/89537_0s.png)
Android Handler runWithScissors 梳理流程解析
Java使用Preference类保存上一次记录的方法
详解java并发之重入锁-ReentrantLock
Java后台线程操作示例【守护线程】
IDEA 2021.2 激活教程及启动报错问题解决方法
![](https://img.aspxhome.com/file/2023/5/59345_0s.png)
Android 动态加载二维码视图生成快照的示例
![](https://img.aspxhome.com/file/2023/9/84719_0s.png)
JavaFx UI控件与代码间的绑定方法
聊聊Spring——AOP详解(AOP概览)
![](https://img.aspxhome.com/file/2023/8/73128_0s.png)
Java Map集合用法详解
![](https://img.aspxhome.com/file/2023/2/63652_0s.png)
深入解析Java多态进阶学习
![](https://img.aspxhome.com/file/2023/9/73039_0s.jpg)