我正在尝试制作一个将图像文件互相转换的代码,但我也成功了但是我遇到了问题。我使用jpg文件进行测试,当我在程序的帮助下将其转换为图标时,我可以做到,我的桌面上有.ico文件,一切都很好,直到现在。但后来我尝试使用.ico文件作为我的表单的图标进行测试,它给出了“参数'图片'必须是一张可以用作图标的图片”错误。我应该编写一个特殊的代码来转换文件,还是仅用于.ico文件?如果是这样,我该如何解决这个问题呢?感谢您的帮助:)
这是我的整个代码(我也只是为了练习这样做,所以如果你想使用部分或全部代码,你可以):
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
#region ConvertFile
void ConvertFile(string a, ImageFormat b, string c)
{
Image d = Image.FromFile(c);
Bitmap e = new Bitmap(d);
e.Save(path + @"\ConvertedImage." + a, b);
MessageBox.Show(
"Image has been successfully converted! You can find it on your desktop.");
}
#endregion
#region Form1_Load
private void Form1_Load(object sender, EventArgs e)
{
cmbFileTypes.SelectedIndex = 0;
}
#endregion
#region btnBrowse_Click
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
openFileDialog1.InitialDirectory = path;
openFileDialog1.ShowDialog();
txtFolderWay.Text = openFileDialog1.FileName;
if (txtFolderWay.Text != "")
{
try
{
Image image = Image.FromFile(txtFolderWay.Text);
pcbImage.Image = image;
}
catch
{
MessageBox.Show("Choose a proper file type!", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
txtFolderWay.Text = "";
pcbImage.Image = null;
return;
}
}
}
#endregion
#region btnConvert_Click
private void btnConvert_Click(object sender, EventArgs e)
{
string a = txtFolderWay.Text;
#region Error Check
if (txtFolderWay.Text == "")
{
MessageBox.Show("Choose a path!");
return;
}
#endregion
if (cmbFileTypes.SelectedIndex == 0)
{
MessageBox.Show("Choose a file type!", "", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
return;
}
else if (cmbFileTypes.SelectedIndex == 1)
{
ConvertFile("jpg",ImageFormat.Jpeg,a);
}
else if (cmbFileTypes.SelectedIndex == 2)
{
ConvertFile("png",ImageFormat.Png,a);
}
else if (cmbFileTypes.SelectedIndex == 3)
{
ConvertFile("bmp",ImageFormat.Bmp,a);
}
else if (cmbFileTypes.SelectedIndex == 4)
{
ConvertFile("emf",ImageFormat.Emf,a);
}
else if (cmbFileTypes.SelectedIndex == 5)
{
ConvertFile("exif",ImageFormat.Exif,a);
}
else if (cmbFileTypes.SelectedIndex == 6)
{
ConvertFile("gif",ImageFormat.Gif,a);
}
else if (cmbFileTypes.SelectedIndex == 7)
{
ConvertFile("ico",ImageFormat.Icon,a);
}
else if (cmbFileTypes.SelectedIndex == 8)
{
ConvertFile("bmp",ImageFormat.MemoryBmp,a);
}
else if (cmbFileTypes.SelectedIndex == 9)
{
ConvertFile("tiff",ImageFormat.Tiff,a);
}
else if (cmbFileTypes.SelectedIndex == 10)
{
ConvertFile("wmf",ImageFormat.Wmf,a);
}
}
#endregion
}
}