Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chico: Create generate .ico feature #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Build results
[Dd]ebug/
[Rr]elease/
[Ee]xe/
# Visual Studio 2015 cache/options directory
.vs/
*_i.c
Expand Down Expand Up @@ -67,7 +68,7 @@ _UpgradeReport_Files/
Backup/
UpgradeLog.XML

# Include dlls if they�re in the NuGet packages directory
# Include dlls if they�re in the NuGet packages directory
!/packages/*/lib/*.dll
# VS Upgrade stuff #
####################
Expand All @@ -80,7 +81,7 @@ UpgradeLog.XML
*.ReSharper.*
# Packages #
############
# it�s better to unpack these files and commit the raw source
# it�s better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
Expand Down
22 changes: 20 additions & 2 deletions SvgToXaml/DetailWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
xmlns:viewModels="clr-namespace:SvgToXaml.ViewModels"
xmlns:textViewer="clr-namespace:SvgToXaml.TextViewer"
mc:Ignorable="d"
Title="{Binding Filename}" Height="375" Width="300"
Title="{Binding Filename}" Height="500" Width="400"
d:DataContext="{x:Static viewModels:SvgImageViewModel.DesignInstance}">
<Grid>
<TabControl TabStripPlacement="Bottom">
<TabItem Header="View Image">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderThickness="1" BorderBrush="#FF00008A" Margin="2">
<Image x:Name="Image" Source="{Binding PreviewSource}" Stretch="Uniform">
Expand Down Expand Up @@ -52,6 +52,24 @@
<TextBlock Text="Stretch" Foreground="Blue" TextDecorations="Underline" TextAlignment="Center" VerticalAlignment="Center" MouseLeftButtonDown="ToggleStretchClicked"/>
<TextBlock Grid.Row="1" TextAlignment="Center" VerticalAlignment="Center" Text="{Binding ElementName=Image, Path=Stretch}"/>
</Grid>
<StackPanel Orientation="Vertical">
<ComboBox x:Name="cbIconSize">
<ComboBoxItem Content="16x16" Tag="16"/>
<ComboBoxItem Content="32x32" Tag="32"/>
<ComboBoxItem Content="64x64" Tag="64"/>
<ComboBoxItem Content="128x128" Tag="128"/>
<ComboBoxItem Content="256x256" Tag="256"/>
<ComboBoxItem Content="512x512" Tag="512" IsSelected="True"/>
<ComboBoxItem Content="1024x1024" Tag="1024"/>
</ComboBox>
<Button
x:Name="btnGenerateIcon"
Padding="3"
Click="btnGenerateIcon_Click"
>
Save to .ICO...
</Button>
</StackPanel>
</StackPanel>
</Grid>
</TabItem>
Expand Down
29 changes: 28 additions & 1 deletion SvgToXaml/DetailWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SvgToXaml
{
Expand All @@ -11,6 +16,16 @@ namespace SvgToXaml
/// </summary>
public partial class DetailWindow
{
private static readonly KeyValuePair<int, string>[] iconsSizes = {
new KeyValuePair<int, string>(16, "16 x 16"),
new KeyValuePair<int, string>(32, "32 x 32"),
new KeyValuePair<int, string>(64, "64 x 64"),
new KeyValuePair<int, string>(128, "128 x 128"),
new KeyValuePair<int, string>(256, "256 x 256"),
new KeyValuePair<int, string>(512, "512 x 512"),
new KeyValuePair<int, string>(1024, "1024 x 1024")
};

public DetailWindow()
{
InitializeComponent();
Expand All @@ -28,5 +43,17 @@ private void ToggleStretchClicked(object sender, MouseButtonEventArgs e)
idx = (idx + 1) % values.Count;
Image.Stretch = values[idx];
}

private void btnGenerateIcon_Click(object sender, RoutedEventArgs e)
{
int sizeOfIcon = int.Parse(((System.Windows.FrameworkElement)this.cbIconSize.SelectedItem).Tag.ToString());
Icon icon = MakeIcon.ToIcon(this.Image.Source,sizeOfIcon);
SaveFileDialog fd = new SaveFileDialog() { Title = "Escolha o arquivo de icone", AddExtension= true, Filter = "Ico Files (*.ico)|*.ico", OverwritePrompt = true, ValidateNames = true };
if (fd.ShowDialog()==true)
using (FileStream fs = new FileStream(fd.FileName, FileMode.Create)){
icon.Save(fs);
}

}
}
}
45 changes: 45 additions & 0 deletions SvgToXaml/MakeIcon/MakeIcon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using Point = System.Windows.Point;
using Size = System.Windows.Size;
using System.Drawing.Imaging;
using System.IO;

namespace SvgToXaml
{
public static class MakeIcon
{
public static Icon ToIcon(this ImageSource imageSource, int size = 64)
{
if (imageSource == null) return null;

System.Windows.Controls.Image image = new System.Windows.Controls.Image();
image.Source = imageSource;
image.Arrange(new Rect(0, 0, size, size));
image.UpdateLayout();

RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(size, size, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(image);

PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

MemoryStream stream = new MemoryStream();
encoder.Save(stream);

Bitmap bmp = new Bitmap(stream);
IntPtr Hicon = bmp.GetHicon();
Icon newIcon = Icon.FromHandle(Hicon);

return newIcon;
}
}
}
2 changes: 2 additions & 0 deletions SvgToXaml/SvgToXaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -76,6 +77,7 @@
<Compile Include="DetailWindow.xaml.cs">
<DependentUpon>DetailWindow.xaml</DependentUpon>
</Compile>
<Compile Include="MakeIcon\MakeIcon.cs" />
<Compile Include="Infrastructure\InvertableBooleanToVisibilityConverter.cs" />
<Compile Include="Infrastructure\HConsoleHelper.cs" />
<Compile Include="Infrastructure\DispatcherExtensions.cs" />
Expand Down
1 change: 1 addition & 0 deletions SvgToXaml/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AvalonEdit" version="5.0.2" targetFramework="net45" />
<package id="System.Drawing.Common" version="7.0.0" targetFramework="net462" />
</packages>
Binary file not shown.
Binary file added packages/System.Drawing.Common.7.0.0/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions packages/System.Drawing.Common.7.0.0/LICENSE.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The MIT License (MIT)

Copyright (c) .NET Foundation and Contributors

All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file not shown.
Loading