using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
namespace ConsoleApplication3_ThumbnailImg
{
class Program
{
const int HEIGHT = 190;
const int WIDTH = 190;
static void Main(string[] args)
{
string srcImgPath = @"G:\photoes\20060725\Picture 057.jpg";
string targetFolder = @"E:\Diary\Test\thumbnail";
SetThumbnail_1(srcImgPath, targetFolder);
SetThumbnail_2(srcImgPath, targetFolder);
SetThumbnail_3(srcImgPath, targetFolder);
Console.Read();
}
static void SetThumbnail_1(string srcImgPath, string targetFolder)
{
using (Bitmap source = new Bitmap(srcImgPath))
{
// return the source image if it's smaller than the designated thumbnail
int wi, hi;
wi = WIDTH;
hi = HEIGHT;
// maintain the aspect ratio despite the thumbnail size parameters
if (source.Width > source.Height)
{
wi = WIDTH;
hi = (int)(source.Height * ((decimal)WIDTH / source.Width));
}
else
{
hi = HEIGHT;
wi = (int)(source.Width * ((decimal)HEIGHT / source.Height));
}
using (Image thumb = source.GetThumbnailImage(wi, hi, null, IntPtr.Zero))
{
string targetPath = Path.Combine(targetFolder, "th_1.jpg");
thumb.Save(targetPath);
}
}
}
static void SetThumbnail_2(string srcImgPath, string targetFolder)
{
using (Bitmap source = new Bitmap(srcImgPath))
{
// return the source image if it's smaller than the designated thumbnail
int wi, hi;
wi = WIDTH;
hi = HEIGHT;
// maintain the aspect ratio despite the thumbnail size parameters
if (source.Width > source.Height)
{
wi = WIDTH;
hi = (int)(source.Height * ((decimal)WIDTH / source.Width));
}
else
{
hi = HEIGHT;
wi = (int)(source.Width * ((decimal)HEIGHT / source.Height));
}
// original code that creates lousy thumbnails
// System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
using (System.Drawing.Bitmap thumb = new Bitmap(wi, hi))
{
using (Graphics g = Graphics.FromImage(thumb))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, wi, hi);
g.DrawImage(source, 0, 0, wi, hi);
}
string targetPath = Path.Combine(targetFolder, "th_2.jpg");
thumb.Save(targetPath);
}
}
}
static void SetThumbnail_3(string srcImgPath, string targetFolder)
{
//Configure JPEG Compression Engine
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1];
quality[0] = 75;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
for (int x = 0; x source.Height)
{
wi = WIDTH;
hi = (int)(source.Height * ((decimal)WIDTH / source.Width));
}
else
{
hi = HEIGHT;
wi = (int)(source.Width * ((decimal)HEIGHT / source.Height));
}
// original code that creates lousy thumbnails
// System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
using (System.Drawing.Bitmap thumb = new Bitmap(wi, hi))
{
using (Graphics g = Graphics.FromImage(thumb))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, wi, hi);
g.DrawImage(source, 0, 0, wi, hi);
}
string targetPath = Path.Combine(targetFolder, "th_3.jpg");
thumb.Save(targetPath, jpegICI, encoderParams);
}
}
}
}
}
生成高品质小空间的缩略图C#代码
来源:这里教程网
时间:2026-02-21 13:01:28
作者:
编辑推荐:
- 生成高品质小空间的缩略图C#代码02-21
- c#异步Socket Tcp服务器实现02-21
- C#测试代码执行时间的方法02-21
- C#图像处理绘制公章02-21
- 常用的C#类02-21
- .Net Core 之 图形验证码02-21
- C#压缩解压帮助类02-21
- C# 如何调用 C++ DLL中的函数接口和回调函数02-21
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- C#图像处理绘制公章
C#图像处理绘制公章
26-02-21 - .Net Core 之 图形验证码
.Net Core 之 图形验证码
26-02-21 - C#中下限非零的数组解析
C#中下限非零的数组解析
26-02-21 - 摩斯密码(Morse code) C#实现
摩斯密码(Morse code) C#实现
26-02-21 - C#使用记秒表检查程序运行时间
C#使用记秒表检查程序运行时间
26-02-21 - 基于C#的百度图片批量下载工具
基于C#的百度图片批量下载工具
26-02-21 - 《超级马力欧银河大电影》角色阵容泄露,瓦力欧有望联手库巴 Jr.登场
《超级马力欧银河大电影》角色阵容泄露,瓦力欧有望联手库巴 Jr.登场
26-02-21 - LibreOffice 抨击 OnlyOffice 是“伪开源”,联手微软裹挟用户
- 任天堂 GBA 经典复刻《宝可梦 火红/叶绿》上架 Switch 商店,2 月 27 日正式发售
- 索尼出手叫停粉丝重制《血源诅咒》项目,系列游戏前景渺茫
索尼出手叫停粉丝重制《血源诅咒》项目,系列游戏前景渺茫
26-02-21
