fixing image Convertion from icon (dotnet/corefx#38108)
authorAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Fri, 31 May 2019 19:02:41 +0000 (12:02 -0700)
committerGitHub <noreply@github.com>
Fri, 31 May 2019 19:02:41 +0000 (12:02 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/fd90e2845c688115d1ddb54a05a2c16a7f8710a5

src/libraries/System.Windows.Extensions/src/System/Drawing/ImageConverter.cs
src/libraries/System.Windows.Extensions/tests/System.Windows.Extensions.Tests.csproj
src/libraries/System.Windows.Extensions/tests/System/Drawing/ImageConverterTests.cs

index d192882..96370f9 100644 (file)
@@ -20,7 +20,7 @@ namespace System.Drawing
 
         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)
@@ -30,6 +30,11 @@ namespace System.Drawing
 
         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.");
index 93712ea..bd20d5f 100644 (file)
@@ -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'">
@@ -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>
index 9f2ec02..72bf4d4 100644 (file)
@@ -33,6 +33,22 @@ namespace System.ComponentModel.TypeConverterTests
             _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()
         {
@@ -55,6 +71,8 @@ namespace System.ComponentModel.TypeConverterTests
             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");