這篇文章主要講解了C#實現圖片格式轉換的方法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
在日常工作中,經常需要不同格式的圖片,有時還需要進行圖片格式的相互轉換,本文以一個簡單的小例子,簡述圖片格式轉換的常見方法,僅供學習分享使用,如有不足之處,還請指正。
涉及知識點
示例效果圖
圖片轉換器的示例效果圖如下:

核心代碼
打開圖片
/// <summary>
/// 打開圖片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpen_Click(object sender, EventArgs e)
{
this.fileDialog.Filter = fileFilter;
this.fileDialog.Multiselect = true;
this.fileDialog.CheckFileExists = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string[] fileNames = this.fileDialog.FileNames;
foreach(string fileName in fileNames)
{
Bitmap bmp = new Bitmap(fileName);
//保存圖片名稱
bmp.Tag = Path.GetFileNameWithoutExtension(fileName);
PictureBox box = new PictureBox();
box.Image = bmp;
box.Width = 105;
box.Height = 150;
box.BorderStyle = BorderStyle.FixedSingle;
box.Padding = new Padding(2);
this.flowPnl.Controls.Add(box);
}
this.txtFile.Text = Path.GetDirectoryName(fileNames[0]);
}
}轉換圖片格式
/// <summary>
/// 轉換圖片
/// </summary>
private void convertImage(string dir, string filter,Bitmap bmp)
{
string filePath = Path.Combine(dir, string.Format("{0}.{1}", bmp.Tag.ToString(), filter.ToLower()));
switch (filter)
{
case "JPG":
bmp.Save(filePath, ImageFormat.Jpeg);
break;
case "PNG":
bmp.Save(filePath, ImageFormat.Png);
break;
case "GIF":
bmp.Save(filePath, ImageFormat.Gif);
break;
case "BMP":
bmp.Save(filePath, ImageFormat.Bmp);
break;
case "ICO":
Stream stream = File.Create(filePath);
Icon icon = Icon.FromHandle(bmp.GetHicon());
icon.Save(stream); // save the icon
stream.Close();
break;
}
}看完上述內容,是不是對C#實現圖片格式轉換的方法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。