WPF学习-生成ico图标(菜鸟笔记)
声明:由于.net下没有IconBitmapEncoder类.其实还是用GDI+来做的.只不过是在Wpf环境下.
首先上图:
另附代码:
代码
<Canvas Margin="0,0,0,0">
<Image x:Name="imgJpeg" Canvas.Left="90" Canvas.Top="62" Height="150" Stretch="Fill" Width="200" />
<TextBlock Text="原始图像:" Height="16" Canvas.Left="65" Canvas.Top="231" Width="51"></TextBlock>
<TextBox x:Name="txtImage" Width="138" Height="21.837" Canvas.Left="127" Canvas.Top="228" IsReadOnly="True"></TextBox>
<Button x:Name="btnOk" Content="浏览" Width="50" Height="21.837" Canvas.Left="283" Canvas.Top="228" Click="btnOk_Click"></Button>
<TextBlock Text="目标尺寸:" Height="16" Canvas.Left="65.11" Canvas.Top="267" Width="50.89"></TextBlock>
<ComboBox x:Name="cmbSize" Height="22" Canvas.Left="127" Canvas.Top="265" Width="138" SelectionChanged="cmbSize_SelectionChanged">
<ComboBoxItem DataContext="1" Selector.IsSelected="True">16*16</ComboBoxItem>
<ComboBoxItem DataContext="2">32*32</ComboBoxItem>
<ComboBoxItem DataContext="3">64*64</ComboBoxItem>
</ComboBox>
<Button x:Name="btnMake" Content="生成ico图标" Height="22" Canvas.Left="283" Canvas.Top="265" Width="70.84" Click="btnMake_Click"></Button>
</Canvas>
后台代码:
代码
public partial class IconMake : Window
{
Bitmap srcBitmap = null;//用于保存原图
System.Drawing.Size size;//用于保存目标图标的大小
public IconMake()
{
InitializeComponent();
}
private void cmbSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cmbSize.SelectedIndex == 0)
size = new System.Drawing.Size(16, 16);
else if (cmbSize.SelectedIndex == 1)
size = new System.Drawing.Size(32, 32);
else if (cmbSize.SelectedIndex == 2)
size = new System.Drawing.Size(64, 64);
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
//弹出对话框
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = "F:\\";
dlg.Filter = "图片(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";
dlg.ValidateNames = true;
dlg.CheckFileExists = true;
dlg.CheckPathExists = true;
//Nullable<bool> result = dlg.ShowDialog();
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtImage.Text = dlg.FileName;
srcBitmap = new Bitmap(dlg.FileName);
imgJpeg.Source = new BitmapImage(new Uri(txtImage.Text, UriKind.RelativeOrAbsolute));
}
}
private void btnMake_Click(object sender, RoutedEventArgs e)
{
try
{
if (txtImage.Text != "" && cmbSize.Text != "")
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "图标(*.ico)|*.ico";
dlg.CheckPathExists = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileStream fs = new FileStream(dlg.FileName, FileMode.Create);
System.Drawing.Icon icon = ConvertBitmap2Ico(srcBitmap);
//将Icon保存的指定的输出
icon.Save(fs);
fs.Close();
}
}
}
catch (Exception err)
{
System.Windows.MessageBox.Show(err.ToString());
}
}
/// <summary>
/// 实现bitmap到ico的转换
/// </summary>
/// <param name="bitmap">原图</param>
/// <returns>转换后的指定大小的图标</returns>
private Icon ConvertBitmap2Ico(Bitmap bitmap)
{
Bitmap icoBitmap = new Bitmap(bitmap, size);
//获得原位图的图标句柄
var a = icoBitmap.GetHicon();
//从图标的指定WINDOWS句柄创建Icon
System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(a);
return icon;
}
}
好了.有几点需要注意的地方.
1.首先我这里的对话框使用的winform下的.所以首先要引用System.Windows.Forms;另外还需引用using System.Drawing;
顺便说一下.wpf下貌似可以用两种类型的对话框.一种是System.Win32下,一种是System.Windows.Forms.有点小区别.win32的对话框边框是旧的样式.windows.forms下的对话框是win7主题样式.还有一个区别,DialogResult返回的结果不同.具体的大家可以自己测试下.
2.由于这里采用的是获取句柄方式转换的.所以图片会失真..