Skip to content

Commit b43a472

Browse files
committed
Added example 6
1 parent e764ac4 commit b43a472

File tree

11 files changed

+256
-0
lines changed

11 files changed

+256
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ These example C++ and C# projects show how to use the Windows Imaging Component
2727
**Language:** C++
2828
**Description:** Shows how to read image metadata using the Windows Imaging Component (WIC).
2929

30+
## Example 6
31+
**Language:** C#
32+
**Description:** Similar as example 4 but it uses the NuGet package [stakx.WIC](https://www.nuget.org/packages/stakx.WIC/) instead of the Microsoft WicCop interop library.
33+
3034
## License
3135
Released under the [MIT license](https://en.wikipedia.org/wiki/MIT_License).

clean.bat

+4
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ cd ..
1616

1717
cd example_5
1818
call clean.bat
19+
cd ..
20+
21+
cd example_6
22+
call clean.bat
1923
cd ..

example_6/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
</configuration>

example_6/Program.cs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

example_6/Properties/AssemblyInfo.cs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.Reflection;
28+
using System.Runtime.CompilerServices;
29+
using System.Runtime.InteropServices;
30+
31+
// General Information about an assembly is controlled through the following
32+
// set of attributes. Change these attribute values to modify the information
33+
// associated with an assembly.
34+
[assembly: AssemblyTitle("WicClient")]
35+
[assembly: AssemblyDescription("")]
36+
[assembly: AssemblyConfiguration("")]
37+
[assembly: AssemblyCompany("")]
38+
[assembly: AssemblyProduct("WicClient")]
39+
[assembly: AssemblyCopyright("Copyright © 2017")]
40+
[assembly: AssemblyTrademark("")]
41+
[assembly: AssemblyCulture("")]
42+
43+
// Setting ComVisible to false makes the types in this assembly not visible
44+
// to COM components. If you need to access a type in this assembly from
45+
// COM, set the ComVisible attribute to true on that type.
46+
[assembly: ComVisible(false)]
47+
48+
// The following GUID is for the ID of the typelib if this project is exposed to COM
49+
[assembly: Guid("6d6602dd-32c7-4cde-bfe4-0a09b573dbe7")]
50+
51+
// Version information for an assembly consists of the following four values:
52+
//
53+
// Major Version
54+
// Minor Version
55+
// Build Number
56+
// Revision
57+
//
58+
// You can specify all the values or you can default the Build and Revision Numbers
59+
// by using the '*' as shown below:
60+
// [assembly: AssemblyVersion("1.0.*")]
61+
[assembly: AssemblyVersion("1.0.0.0")]
62+
[assembly: AssemblyFileVersion("1.0.0.0")]

example_6/WicClient.csproj

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{6D6602DD-32C7-4CDE-BFE4-0A09B573DBE7}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>WicClient</RootNamespace>
11+
<AssemblyName>WicClient</AssemblyName>
12+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="stakx.WIC, Version=0.1.0.0, Culture=neutral, processorArchitecture=MSIL">
37+
<HintPath>packages\stakx.WIC.0.1.0\lib\net40\stakx.WIC.dll</HintPath>
38+
<Private>True</Private>
39+
</Reference>
40+
<Reference Include="System" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="Program.cs" />
44+
<Compile Include="Properties\AssemblyInfo.cs" />
45+
</ItemGroup>
46+
<ItemGroup>
47+
<None Include="App.config" />
48+
<None Include="packages.config" />
49+
</ItemGroup>
50+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
51+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
52+
Other similar extension points exist, see Microsoft.Common.targets.
53+
<Target Name="BeforeBuild">
54+
</Target>
55+
<Target Name="AfterBuild">
56+
</Target>
57+
-->
58+
</Project>

example_6/WicClient.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WicClient", "WicClient.csproj", "{6D6602DD-32C7-4CDE-BFE4-0A09B573DBE7}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{6D6602DD-32C7-4CDE-BFE4-0A09B573DBE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{6D6602DD-32C7-4CDE-BFE4-0A09B573DBE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{6D6602DD-32C7-4CDE-BFE4-0A09B573DBE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{6D6602DD-32C7-4CDE-BFE4-0A09B573DBE7}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

example_6/clean.bat

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
rmdir /S /Q .vs
2+
rmdir /S /Q bin
3+
rmdir /S /Q obj

example_6/packages.config

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="stakx.WIC" version="0.1.0" targetFramework="net461" />
4+
</packages>
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)