1
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
+ //
3
+ // MIT License
4
+ //
5
+ // Copyright(c) 2017 René Slijkhuis
6
+ //
7
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ // of this software and associated documentation files (the "Software"), to deal
9
+ // in the Software without restriction, including without limitation the rights
10
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ // copies of the Software, and to permit persons to whom the Software is
12
+ // furnished to do so, subject to the following conditions:
13
+ //
14
+ // The above copyright notice and this permission notice shall be included in all
15
+ // copies or substantial portions of the Software.
16
+ //
17
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ // SOFTWARE.
24
+ //
25
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
+
27
+ using System ;
28
+ using stakx . WIC ;
29
+
30
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
31
+
32
+ namespace WicClient
33
+ {
34
+ class Program
35
+ {
36
+ // The GUID definition below should be part of the 'stakx.WIC' package.
37
+ private static readonly Guid GUID_WICPixelFormat24bppRGB = new Guid ( 0x6fddc324 , 0x4e03 , 0x4bfe , 0xb1 , 0x85 , 0x3d , 0x77 , 0x76 , 0x8d , 0xc9 , 0x0d ) ;
38
+
39
+ static void Main ( string [ ] args )
40
+ {
41
+ // This example requires the NuGet package 'stakx.WIC'.
42
+ // https://www.nuget.org/packages/stakx.WIC/
43
+ // https://github.com/stakx/WIC
44
+
45
+ const string filename = @"<filename>" ;
46
+
47
+ IWICImagingFactory factory = new WICImagingFactory ( ) ;
48
+ IWICBitmapDecoder decoder = null ;
49
+ IWICBitmapFrameDecode frame = null ;
50
+ IWICFormatConverter formatConverter = null ;
51
+
52
+ decoder = factory . CreateDecoderFromFilename ( filename , IntPtr . Zero , StreamAccessMode . GENERIC_READ ,
53
+ WICDecodeOptions . WICDecodeMetadataCacheOnDemand ) ;
54
+
55
+ int count = decoder . GetFrameCount ( ) ;
56
+
57
+ frame = decoder . GetFrame ( 0 ) ;
58
+
59
+ int width = 0 ;
60
+ int height = 0 ;
61
+ frame . GetSize ( out width , out height ) ;
62
+
63
+ Guid pixelFormat = frame . GetPixelFormat ( ) ;
64
+
65
+ // The frame can use many different pixel formats.
66
+ // You can copy the raw pixel values by calling "frame.CopyPixels( )".
67
+ // This method needs a buffer that can hold all bytes.
68
+ // The total number of bytes is: width x height x bytes per pixel
69
+
70
+ // The disadvantage of this solution is that you have to deal with all possible pixel formats.
71
+
72
+ // You can make your life easy by converting the frame to a pixel format of
73
+ // your choice. The code below shows how to convert the pixel format to 24-bit RGB.
74
+
75
+ formatConverter = factory . CreateFormatConverter ( ) ;
76
+
77
+ formatConverter . Initialize ( frame ,
78
+ GUID_WICPixelFormat24bppRGB ,
79
+ WICBitmapDitherType . WICBitmapDitherTypeNone ,
80
+ null ,
81
+ 0.0 ,
82
+ WICBitmapPaletteType . WICBitmapPaletteTypeCustom ) ;
83
+
84
+ int bytesPerPixel = 3 ; // Because we have converted the frame to 24-bit RGB
85
+ int stride = width * bytesPerPixel ;
86
+ byte [ ] bytes = new byte [ stride * height ] ;
87
+
88
+ formatConverter . CopyPixels ( IntPtr . Zero , stride , stride * height , bytes ) ;
89
+ }
90
+ }
91
+ }
92
+
93
+ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0 commit comments