forked from microsoft/WPF-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.cs
109 lines (89 loc) · 3.57 KB
/
App.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// // Copyright (c) Microsoft. All rights reserved.
// // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Security;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
[assembly: SecurityTransparent]
namespace GIFEncoderAndDecoder
{
public class App : Application
{
private Window _mainWindow;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
CreateAndShowMainWindow();
}
private void CreateAndShowMainWindow()
{
// Create the application's main window
_mainWindow = new Window {Title = "GIF Imaging Sample"};
var mySv = new ScrollViewer();
var width = 128;
var height = width;
var stride = width/8;
var pixels = new byte[height*stride];
// Define the image palette
var myPalette = BitmapPalettes.WebPalette;
// Creates a new empty image with the pre-defined palette
var image = BitmapSource.Create(
width,
height,
96,
96,
PixelFormats.Indexed1,
myPalette,
pixels,
stride);
var stream = new FileStream("new.gif", FileMode.Create);
var encoder = new GifBitmapEncoder();
var myTextBlock = new TextBlock {Text = "Codec Author is: " + encoder.CodecInfo.Author};
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
// Open a Stream and decode a GIF image
Stream imageStreamSource = new FileStream("tulipfarm.gif", FileMode.Open, FileAccess.Read, FileShare.Read);
var decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
// Draw the Image
var myImage = new Image
{
Source = bitmapSource,
Stretch = Stretch.None,
Margin = new Thickness(20)
};
// Open a Uri and decode a GIF image
var myUri = new Uri("tulipfarm.gif", UriKind.RelativeOrAbsolute);
var decoder2 = new GifBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.Default);
BitmapSource bitmapSource2 = decoder2.Frames[0];
// Draw the Image
var myImage2 = new Image
{
Source = bitmapSource2,
Stretch = Stretch.None,
Margin = new Thickness(20)
};
// Define a StackPanel to host the decoded GIF images
var myStackPanel = new StackPanel
{
Orientation = Orientation.Vertical,
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch
};
// Add the Image and TextBlock to the parent Grid
myStackPanel.Children.Add(myImage);
myStackPanel.Children.Add(myImage2);
myStackPanel.Children.Add(myTextBlock);
// Add the StackPanel as the Content of the Parent Window Object
mySv.Content = myStackPanel;
_mainWindow.Content = mySv;
_mainWindow.Show();
}
}
// Define a static entry class
}