Skip to content

Commit 07d87a6

Browse files
committed
add bitmapimage to bitmap converter, add icon saving test (only saves one resolution)
1 parent 131c381 commit 07d87a6

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

PixelArtTool/MainWindow.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@
113113
<Button x:Name="btnSave" ToolTip="Save (Ctrl+S)" Click="OnSaveButton">
114114
<Image Source="/Resources/Buttons/save.png" Width="24" Height="24" RenderOptions.BitmapScalingMode="NearestNeighbor" ToolTip="Save.." />
115115
</Button>
116+
<Button x:Name="btnSaveIco" ToolTip="Save (Ctrl+S)" Click="OnExportIcoButtonClick">
117+
<Image Source="/Resources/Buttons/emptybutton.png" Width="24" Height="24" RenderOptions.BitmapScalingMode="NearestNeighbor" ToolTip="Export .ico" />
118+
</Button>
116119
</ToolBar>
117120

118121
<ToolBar Band="1" BandIndex="1" VerticalAlignment="Top" Background="#FF9C9C9C">

PixelArtTool/MainWindow.xaml.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,5 +1494,20 @@ private void rectSecondaryColor_MouseDown(object sender, MouseButtonEventArgs e)
14941494
((Rectangle)sender).Fill = newcolor;
14951495
}
14961496
}
1497+
1498+
1499+
private void OnExportIcoButtonClick(object sender, RoutedEventArgs e)
1500+
{
1501+
// TODO try this https://stackoverflow.com/a/32530019/5452781
1502+
1503+
// this only saves one size
1504+
var bitmapimage = ConvertWriteableBitmapToBitmapImage(canvasBitmap);
1505+
var bitmap = ConvertBitmapImageToBitmap(bitmapimage);
1506+
System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(bitmap.GetHicon());
1507+
using (var fileStream = File.Create("D:\\myicon.ico"))
1508+
{
1509+
icon.Save(fileStream);
1510+
}
1511+
}
14971512
} // class
14981513
} // namespace

PixelArtTool/Tools.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,20 @@ public static BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wb
516516
return bmImage;
517517
}
518518

519+
// https://stackoverflow.com/a/6484754/5452781
520+
public static System.Drawing.Bitmap ConvertBitmapImageToBitmap(BitmapImage bitmapImage)
521+
{
522+
using (MemoryStream outStream = new MemoryStream())
523+
{
524+
BitmapEncoder enc = new BmpBitmapEncoder();
525+
enc.Frames.Add(BitmapFrame.Create(bitmapImage));
526+
enc.Save(outStream);
527+
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
528+
529+
return new System.Drawing.Bitmap(bitmap);
530+
}
531+
}
532+
519533
public static SolidColorBrush ConvertSystemDrawingColorToSolidColorBrush(System.Drawing.Color c)
520534
{
521535
return new SolidColorBrush(Color.FromArgb(c.A, c.R, c.G, c.B));

0 commit comments

Comments
 (0)