C#實(shí)現(xiàn)多種圖片格式轉(zhuǎn)換的示例詳解
1.目的
實(shí)現(xiàn)多種圖片格式的相互轉(zhuǎn)換,圖片大小可自定義等。
2.知識(shí)點(diǎn)
轉(zhuǎn)換成圖標(biāo)圖像(ico)時(shí),需要獲取圖像句柄,然后根據(jù)句柄生成Ico圖像,否則生成的圖像不能作為應(yīng)用的圖標(biāo)使用。
IntPtr hwd = bitmap.GetHicon(); Icon icon = Icon.FromHandle(hwd); icon.Save(fs);
利用反射獲取系統(tǒng)可支持的圖片類(lèi)型,獲取靜態(tài)屬性的值。
ImageFormat format = typeof(ImageFormat).GetProperty(comboBox1.Text).GetValue(null) as ImageFormat;
3.效果展示


4.代碼
public partial class Form1 : Form
{
string useExt;
public Form1()
{
InitializeComponent();
}
private void btnSelect_Click(object sender, EventArgs e)
{
using(OpenFileDialog ofd=new OpenFileDialog())
{
ofd.Multiselect = false;
if (useExt != null)
{
ofd.Filter = useExt;
}
if(ofd.ShowDialog()== DialogResult.OK)
{
txtFilePath.Text = ofd.FileName;
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
using(SaveFileDialog sfd=new SaveFileDialog())
{
sfd.CheckPathExists = true;
string ext=comboBox1.Text;
if (comboBox1.Text.ToUpper() == "JPEG")
{
ext = "jpg";
}
if (comboBox1.Text.ToUpper() == "ICON")
{
ext = "ico";
}
sfd.Filter = $"*.{ext}文件|*.{ext}";
if(sfd.ShowDialog()== DialogResult.OK)
{
txtSavePath.Text = sfd.FileName;
}
}
}
private void button3_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(txtFilePath.Text)|| string.IsNullOrEmpty(txtSavePath.Text))
{
MessageBox.Show("請(qǐng)先選擇文件路徑","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string sizeStr = comboBox2.Text;
string[] wandh = sizeStr.Split('*');
double width,height;
if (wandh.Length == 2)
{
if(double.TryParse(wandh[0],out width) && double.TryParse(wandh[1],out height))
{
Image img = Image.FromFile(txtFilePath.Text);
Size size ;
if ((width ==1)&& (height == 1))
{
size = new Size(img.Width, img.Height);
}
else
{
size = new Size((int)width, (int)height);
}
Bitmap bitmap = new Bitmap(img,size);
ImageFormat format = typeof(ImageFormat).GetProperty(comboBox1.Text).GetValue(null) as ImageFormat;
using(FileStream fs=new FileStream(txtSavePath.Text, FileMode.Create))
{
if (format == ImageFormat.Icon)
{
IntPtr hwd = bitmap.GetHicon();
Icon icon = Icon.FromHandle(hwd);
icon.Save(fs);
}
else
{
bitmap.Save(fs, format);
}
}
MessageBox.Show("已保存至:"+txtSavePath.Text,"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("目標(biāo)尺寸參數(shù)格式異常", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("目標(biāo)尺寸參數(shù)數(shù)量異常", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
//獲取所有可供轉(zhuǎn)換的類(lèi)型
foreach (var item in typeof(ImageFormat).GetProperties())
{
comboBox1.Items.Add(item.Name);
list.Add($"*.{item.Name}文件|*.{item.Name}");
}
list.Add("*.jpg文件|*.jpg");
list.Reverse();
useExt = string.Join("|", list);
if (comboBox1.Items.Count > 0)
comboBox1.SelectedIndex = comboBox1.Items.Count-1;
comboBox2.SelectedIndex = 0;
}
}到此這篇關(guān)于C#實(shí)現(xiàn)多種圖片格式轉(zhuǎn)換的示例詳解的文章就介紹到這了,更多相關(guān)C#圖片格式轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何用C#獲取計(jì)算機(jī)詳細(xì)的軟件和硬件信息
我們應(yīng)該都知道System.Management提供的類(lèi)可以用于讀取本地計(jì)算機(jī)設(shè)備的各種數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于如何用C#獲取計(jì)算機(jī)詳細(xì)的軟件和硬件信息的相關(guān)資料,需要的朋友可以參考下2022-12-12
c# 抓取Web網(wǎng)頁(yè)數(shù)據(jù)分析
通過(guò)程序自動(dòng)的讀取其它網(wǎng)站網(wǎng)頁(yè)顯示的信息,類(lèi)似于爬蟲(chóng)程序。比方說(shuō)我們有一個(gè)系統(tǒng),要提取BaiDu網(wǎng)站上歌曲搜索排名。分析系統(tǒng)在根據(jù)得到的數(shù)據(jù)進(jìn)行數(shù)據(jù)分析。為業(yè)務(wù)提供參考數(shù)據(jù)。2008-11-11
C#使用QRCode生成海報(bào)圖并嵌入定位帶logo的二維碼
這篇文章主要為大家詳細(xì)介紹了C#如何使用QRCode生成海報(bào)圖并嵌入定位帶logo的二維碼,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2024-03-03

