C#使用Magick.NET
进行图片格式转换,修改尺寸(.ico .jpg .png .gif .bmp
),解决png
转jpg
透明变黑底问题
看了许多其他博客格式转换的代码,试过了才发现很多转成ico的图片虽然可以正常查看,但是压根无法使用,最简单的连用来作为Wiform
软件的图标都不行,最后呢参考py
代码发现py
的PythonMagick
这个包挺好用的,仔细查看后发现C#
也有类似的包,Magick.NET
,废话不多少,直接进入正题吧!
1.安装NuGet
包
2.核心代码
试了几种格式,gif/jpg/png/ico
都可以互转,bmp
可以转成其他的,其他转bmp
会报错,bmp
不用也无所谓了,这年头基本没人用bmp
,就跟微软的ie一样,人人喊打!
new Thread(() => {
try {
button1.Enabled = false;
using (MagickImage image = new MagickImage(filePath)) {
//格式
string file_format = file_format_comboBox.SelectedItem.ToString().Trim();
if (file_format.EndsWith("ico")) {
file_format = ".ico";
} else if (file_format.EndsWith("png")) {
file_format = ".png";
} else if (file_format.EndsWith("jpg")) {
file_format = ".jpg";
//质量百分比
string quality = quality_trackBar.Value.ToString();
//原图质量的百分比进行压缩
image.Quality = Convert.ToInt32(quality);
//将透明色更改成白色(这里不指定默认是黑色)
image.Opaque(Color.Transparent, Color.White);
}
string width_text = width_textBox.Text.Trim();
string height_text = height_textBox.Text.Trim();
if (!defaultSize_checkBox.Checked) {
if (!Regex.IsMatch(width_text, "^[1-9]\\d*$")) {
MessageBox.Show("宽度错误");
return;
}
if (!Regex.IsMatch(height_text, "^[1-9]\\d*$")) {
MessageBox.Show("高度错误");
return;
}
int new_width = Convert.ToInt32(width_text);
int new_height = Convert.ToInt32(height_text);
if (file_format.EndsWith("ico")) {
if (new_width > 256 || new_height > 256) {
MessageBox.Show("ico格式宽高不可超过256", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
if (new_width <= img_width && new_height <= img_height) {
//缩小:设定的宽高都比原图小
int max = new_width > new_height ? new_width : new_height;
image.Resize(max, max);
} else {
//等比例放大
decimal d1 = Convert.ToDecimal(new_width) / Convert.ToDecimal(img_width);
decimal d2 = Convert.ToDecimal(new_height) / Convert.ToDecimal(img_height);
decimal maxMult = d1 > d2 ? d1 : d2;
int max_X = Convert.ToInt32(maxMult * Convert.ToDecimal(img_width));
int max_Y = Convert.ToInt32(maxMult * Convert.ToDecimal(img_height));
image.Resize(max_X, max_Y);
}
double[] ProjectTransform =
{
0,0,0,0, //左上角
image.Width,0,new_width,0, //右上角
0,image.Height,0,new_height, //左下角
image.Width,image.Height,new_width,new_height//右下角
};
//按照ProjectTransform的坐标点进行拉伸或缩放
image.Distort(DistortMethod.Perspective, ProjectTransform);
//因为按大小进行拉伸或缩放后,其他区域会糊掉,所以要裁剪出我们的所需的大小,既缩放拉伸或的清楚区域
image.Crop(new_width, new_height);
} else {
image.Resize(Convert.ToInt32(width_text), Convert.ToInt32(height_text));
}
string newFileName = @"C:\Users\Administrator\Desktop\new_" + Path.GetFileNameWithoutExtension(filePath) + file_format;
try {
image.Write(newFileName);
} catch (Exception ee) {
if (ee.Message.Contains("width or height exceeds limit")) {
MessageBox.Show("宽度或高度超过限制", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
} else {
MessageBox.Show(ee.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
newFile_label.Text = newFileName;
}
} catch (Exception ee) {
Console.WriteLine(ee);
} finally {
button1.Enabled = true;
}
}).Start();
关于png
转jpg
透明背景变成黑色的问题解决代码:
//将透明色替换成白色(这里不指定默认是黑色)
image.Opaque(Color.Transparent, Color.White);
还有个地方需要注意一下,就是修改尺寸的时候,如果宽或者高大于原来的图片尺寸,那么需要先按比例将此MagickImage
放大(Resize
),然后进行拉伸(Distort
),最后在进行裁剪.