Quick Snip - 轻量级图像调整器






3.78/5 (11投票s)
2005年5月8日
2分钟阅读

65258

1137
Quick Snip 是一个极其轻量级的应用程序,用于从大图像创建“网络尺寸”图片
引言
Quick Snip 是一个极其轻量级的应用程序,用于从大图像创建“网络尺寸”图片。 我经常想用数码相机拍照,只想获得一张最大宽度为 600 的图片,这样我就可以轻松地通过电子邮件发送给我的朋友。 像 GIMP 和 Photoshop 这样的常见图像软件开销太大 - 反复打开、选择设置和选择目录令人讨厌。 Quick Snip 通过“发送到”菜单访问,并将 JPG 放置在同一目录下,文件名 + "_resized.jpg"。 一步完成,没有麻烦,没有烦恼。
背景
该程序简要介绍了如何获取图像并调整其大小。 我使用该程序的更强大的版本嵌入到我的网站中,以自动调整图库集合的图像大小。 它还涉及如何进行基本的控制台程序操作,例如读/写到控制台和传递参数。 阅读和理解这篇文章应该非常容易,并且可以大大扩展您的编程选项。
使用代码
重要说明
如果您想从头开始创建一个控制台应用程序,您需要手动向 System.Drawing
添加一个“.NET 引用”。 (引用 -> 添加引用 -> System.drawing | 确定)。
为了将程序注册到“发送到”中使用,您需要向 Send to 目录添加一个快捷方式。 在我的电脑上(Windows 2003),该目录位于 \Documents and Settings\User\SendTo。
现在开始编码!
当选择一个文件并使用“发送到”命令时,每个文件名都作为后续命令行参数传递。 我们可以轻松地遍历每个文件。
foreach (string FileName in args)
{
// quickly slap "_resized.jpg" at the end.
string NewFileName = FileName.Substring(0,
(FileName.Length -4)) + "_resized.jpg"
ResizeImage(600,600, FileName, NewFileName);
}
接下来,我们需要一段代码来处理图像调整大小
public static void ResizeImage(int MaxWidth,
int MaxHeight, string FileName, string NewFileName)
{
// load up the image, figure out a "best fit" resize,
// and then save that new image
Bitmap OriginalBmp =
(System.Drawing.Bitmap)Image.FromFile(FileName).Clone();
Size ResizedDimensions =
GetDimensions(MaxWidth, MaxHeight, ref OriginalBmp);
Bitmap NewBmp = new Bitmap(OriginalBmp, ResizedDimensions);
NewBmp.Save(NewFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
确定最佳尺寸的实际算法由一个名为 GetDimensions
的函数处理。 GetDimensions
是一个非常简单的函数,用于返回调整大小的图像的一组合理的尺寸。
public static Size GetDimensions(int MaxWidth, int MaxHeight, ref Bitmap Bmp)
{
int Width;
int Height;
float Multiplier;
Height = Bmp.Height;
Width = Bmp.Width;
// this means you want to shrink an image that is already shrunken!
if (Height <= MaxHeight && Width <= MaxWidth)
return new Size(Width, Height);
// check to see if we can shrink it width first
Multiplier = (float)((float)MaxWidth / (float)Width);
if ((Height * Multiplier)<= MaxHeight)
{
Height = (int)(Height * Multiplier);
return new Size(MaxWidth, Height);
}
// if we can't get our max width, then use the max height
Multiplier = (float)MaxHeight / (float)Height;
Width = (int)(Width * Multiplier);
return new Size(Width, MaxHeight);
}
就是这样!
完整代码
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace QuickSnip
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
// when you use send to it will pass each file
// as it's own string in args
foreach (string FileName in args)
{
try
{
// quickly slap "_resized.jpg" at the end.
string NewFileName = FileName.Substring(0,
(FileName.Length -4)) + "_resized.jpg";
// nice when you have multiple images
Console.WriteLine(FileName);
Console.WriteLine(NewFileName);
// call our thumbnail function
ResizeImage(600,600, FileName, NewFileName);
}
catch (System.Exception ex)
{
// UH OH!!!
Console.Write(ex.Message);
Console.ReadLine();
}
}
}
/// <SUMMARY>
/// This function takes a max width/height
/// and makes a new file based on it
/// </SUMMARY>
/// <PARAM name="MaxWidth">Max width of the new image</PARAM>
/// <PARAM name="MaxHeight">Max Height of the new image</PARAM>
/// <PARAM name="FileName">Original file name</PARAM>
/// <PARAM name="NewFileName">new file name</PARAM>
public static void ResizeImage(int MaxWidth, int MaxHeight,
string FileName, string NewFileName)
{
// load up the image, figure out a "best fit"
// resize, and then save that new image
Bitmap OriginalBmp =
(System.Drawing.Bitmap)Image.FromFile(FileName).Clone();
Size ResizedDimensions =
GetDimensions(MaxWidth, MaxHeight, ref OriginalBmp);
Bitmap NewBmp = new Bitmap(OriginalBmp, ResizedDimensions);
NewBmp.Save(NewFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
/// <SUMMARY>
/// this function aims to give you a best fit
/// for a resize. It assumes width is more important
/// then height. If an image is already smaller
/// then max dimensions it will not resize it.
/// </SUMMARY>
/// <PARAM name="MaxWidth">max width of the new image</PARAM>
/// <PARAM name="MaxHeight">max height of the new image</PARAM>
/// <PARAM name="Bmp">BMP of the current image,
/// passing by ref so fast</PARAM>
/// <RETURNS></RETURNS>
public static Size GetDimensions(int MaxWidth,
int MaxHeight, ref Bitmap Bmp)
{
int Width;
int Height;
float Multiplier;
Height = Bmp.Height;
Width = Bmp.Width;
// this means you want to shrink
// an image that is already shrunken!
if (Height <= MaxHeight && Width <= MaxWidth)
return new Size(Width, Height);
// check to see if we can shrink it width first
Multiplier = (float)((float)MaxWidth / (float)Width);
if ((Height * Multiplier)<= MaxHeight)
{
Height = (int)(Height * Multiplier);
return new Size(MaxWidth, Height);
}
// if we can't get our max width, then use the max height
Multiplier = (float)MaxHeight / (float)Height;
Width = (int)(Width * Multiplier);
return new Size(Width, MaxHeight);
}
}
}
关注点
我编写了许多最终进行图像调整大小和少量图像处理的程序。 我的个人网站使用一个简单的目录扫描器来查找新的图像集合。 当它找到它们时,它会自动创建缩略图和网络尺寸的版本。 我还有另一个程序,它在我的计算机上找到图像,并按计时器随机创建副本、调整大小并重新设置我的壁纸。 总的来说,我发现用强大的结果编写少量的图像处理很容易。
我希望人们发现我的第一篇文章有用,我将开始发布我一些更有趣的代码。
历史
暂无历史记录 ;)