Skip to content

Commit

Permalink
fixing image Convertion from icon (dotnet/corefx#38108)
Browse files Browse the repository at this point in the history
Commit migrated from dotnet/corefx@fd90e28
  • Loading branch information
Anipik authored May 31, 2019
1 parent f72c30b commit 7478a20
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ImageConverter : TypeConverter

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(byte[]);
return sourceType == typeof(byte[]) || sourceType == typeof(Icon);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
Expand All @@ -30,6 +30,11 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is Icon icon)
{
return icon.ToBitmap();
}

if (value is byte[] bytes)
{
Debug.Assert(value != null, "value is null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<ProjectGuid>{AC1A1515-70D8-42E4-9B19-A72F739E974C}</ProjectGuid>
<Configurations>netcoreapp-Windows_NT-Debug;netcoreapp-Windows_NT-Release;netfx-Windows_NT-Debug;netfx-Windows_NT-Release</Configurations>
<SystemComponentmodelTypeconverterTestdataVersion>1.0.1</SystemComponentmodelTypeconverterTestdataVersion>
<SystemDrawingCommonTestdataVersion>1.0.9</SystemDrawingCommonTestdataVersion>
<SystemWindowsExtensionsTestdataVersion>1.0.2</SystemWindowsExtensionsTestdataVersion>
</PropertyGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
Expand All @@ -28,6 +29,9 @@
<SupplementalTestData Include="$(NuGetPackageRoot)system.componentmodel.typeconverter.testdata\$(SystemComponentmodelTypeconverterTestdataVersion)\content\**\*.*">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
</SupplementalTestData>
<SupplementalTestData Include="$(NuGetPackageRoot)system.drawing.common.testdata\$(SystemDrawingCommonTestdataVersion)\content\**\*.*">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
</SupplementalTestData>
<SupplementalTestData Include="$(NuGetPackageRoot)system.windows.extensions.testdata\$(SystemWindowsExtensionsTestdataVersion)\content\**\*.*">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
</SupplementalTestData>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ public ImageConverterTest()
_imgConvFrmTD = (ImageConverter)TypeDescriptor.GetConverter(_image);
}

[ConditionalTheory(Helpers.IsDrawingSupported)]
[InlineData("48x48_multiple_entries_4bit.ico")]
[InlineData("256x256_seven_entries_multiple_bits.ico")]
[InlineData("pngwithheight_icon.ico")]
public void ImageConverterFromIconTest(string name)
{
using (var icon = new Icon(Helpers.GetTestBitmapPath(name)))
{
Bitmap IconBitmap = (Bitmap)_imgConv.ConvertFrom(icon);
Assert.NotNull(IconBitmap);
Assert.Equal(32, IconBitmap.Width);
Assert.Equal(32, IconBitmap.Height);
Assert.Equal(new Size(32, 32), IconBitmap.Size);
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void ImageWithOleHeader()
{
Expand All @@ -55,6 +71,8 @@ public void TestCanConvertFrom()
Assert.True(_imgConv.CanConvertFrom(typeof(byte[])), "byte[] (no context)");
Assert.True(_imgConv.CanConvertFrom(null, typeof(byte[])), "byte[]");
Assert.True(_imgConv.CanConvertFrom(null, _imageBytes.GetType()), "_imageBytes.GetType()");
Assert.True(_imgConv.CanConvertFrom(typeof(Icon)), "Icon (no context)");
Assert.True(_imgConv.CanConvertFrom(null, typeof(Icon)), "Icon");
Assert.False(_imgConv.CanConvertFrom(null, typeof(string)), "string");
Assert.False(_imgConv.CanConvertFrom(null, typeof(Rectangle)), "Rectangle");
Assert.False(_imgConv.CanConvertFrom(null, typeof(Point)), "Point");
Expand Down

0 comments on commit 7478a20

Please sign in to comment.