MSDN表示没有"IcoBitmapEncoder"。我建议您从BitmapFrame
中获取Bitmap
,然后将其转换为图标,就像这样:
BitmapEncoder imgEncoder = new PngBitmapEncoder();
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(32, 32, 96d, 96d, PixelFormats.Pbgra32);
renderBitmap.Render(comboBox1);
var frame = BitmapFrame.Create(renderBitmap);
Bitmap bitmap = GetBitmap(frame);
bitmap.SetResolution(72, 72);
System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(bitmap.GetHicon());
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
icon.Save(fs);
fs.Close();
你可以从这里获得GetBitmap
方法:
static Bitmap GetBitmap(BitmapSource source)
{
Bitmap bmp = new Bitmap(source.PixelWidth, source.PixelHeight,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
BitmapData data = bmp.LockBits(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bmp.UnlockBits(data);
return bmp;
}