[C#] Image 转换成 Icon
2010-05-06 10:35
309 查看
/// <summary>
/// Converts an image into an icon.
/// </summary>
/// <param name="img">The image that shall become an icon</param>
/// <param name="size">The width and height of the icon. Standard
/// sizes are 16x16, 32x32, 48x48, 64x64.</param>
/// <param name="keepAspectRatio">Whether the image should be squashed into a
/// square or whether whitespace should be put around it.</param>
/// <returns>An icon!!</returns>
private Icon MakeIcon(Image img, int size, bool keepAspectRatio)
{
Bitmap square = new Bitmap(size, size); // create new bitmap
Graphics g = Graphics.FromImage(square); // allow drawing to it
int x, y, w, h; // dimensions for new image
if (!keepAspectRatio || img.Height == img.Width)
{
// just fill the square
x = y = 0; // set x and y to 0
w = h = size; // set width and height to size
}
else
{
// work out the aspect ratio
float r = (float)img.Width / (float)img.Height;
// set dimensions accordingly to fit inside size^2 square
if (r > 1)
{ // w is bigger, so divide h by r
w = size;
h = (int)((float)size / r);
x = 0; y = (size - h) / 2; // center the image
}
else
{ // h is bigger, so multiply w by r
w = (int)((float)size * r);
h = size;
y = 0; x = (size - w) / 2; // center the image
}
}
// make the image shrink nicely by using HighQualityBicubic mode
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(img, x, y, w, h); // draw image with specified dimensions
g.Flush(); // make sure all drawing operations complete before we get the icon
// following line would work directly on any image, but then
// it wouldn't look as nice.
return Icon.FromHandle(square.GetHicon());
}
/// Converts an image into an icon.
/// </summary>
/// <param name="img">The image that shall become an icon</param>
/// <param name="size">The width and height of the icon. Standard
/// sizes are 16x16, 32x32, 48x48, 64x64.</param>
/// <param name="keepAspectRatio">Whether the image should be squashed into a
/// square or whether whitespace should be put around it.</param>
/// <returns>An icon!!</returns>
private Icon MakeIcon(Image img, int size, bool keepAspectRatio)
{
Bitmap square = new Bitmap(size, size); // create new bitmap
Graphics g = Graphics.FromImage(square); // allow drawing to it
int x, y, w, h; // dimensions for new image
if (!keepAspectRatio || img.Height == img.Width)
{
// just fill the square
x = y = 0; // set x and y to 0
w = h = size; // set width and height to size
}
else
{
// work out the aspect ratio
float r = (float)img.Width / (float)img.Height;
// set dimensions accordingly to fit inside size^2 square
if (r > 1)
{ // w is bigger, so divide h by r
w = size;
h = (int)((float)size / r);
x = 0; y = (size - h) / 2; // center the image
}
else
{ // h is bigger, so multiply w by r
w = (int)((float)size * r);
h = size;
y = 0; x = (size - w) / 2; // center the image
}
}
// make the image shrink nicely by using HighQualityBicubic mode
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(img, x, y, w, h); // draw image with specified dimensions
g.Flush(); // make sure all drawing operations complete before we get the icon
// following line would work directly on any image, but then
// it wouldn't look as nice.
return Icon.FromHandle(square.GetHicon());
}
相关文章推荐
- c# 转换Image为Icon
- 【C#】无损转换Image为Icon 【C#】组件发布:MessageTip,轻快型消息提示窗 【C#】给无窗口的进程发送消息 【手记】WebBrowser响应页面中的blank开新窗口及window.close关闭本窗体 【手记】调用Process.EnterDebugMode引发异常:并非所有引用的特权或组都分配给呼叫方 【C#】DataRowState演变备忘
- 【C#】无损转换Image为Icon
- C#无损转换Image为Icon的方法
- [C#] Byte[]、Image、Bitmap 之间的相互转换
- C#中如何把Sql Server中的image格式转换成图片
- [C#] Byte[]、Image、Bitmap 之间的相互转换
- C# pdf 转换成 image
- c# Byte[]、Image、Bitmap 之间的相互转换
- C#将image中的显示的图片转换成二进制
- C# byte[]与Image的相互转换
- Image 转换成 Icon
- 把Swing的Icon转换到SWT的Image(续) 推荐
- C#将image中的显示的图片转换成二进制
- 【转】C# byte数组与Image的相互转换
- C#将image中的显示的图片转换成二进制
- C#Image和Icon的相互转化
- C# byte数组与Image相互转换的方法
- [C#] Byte[]、Image、Bitmap 之间的相互转换
- 内存转换Image到Icon格式