Beispiel #1
0
        public void RemovePropertyItem_InvokeBitmapJpg_Success()
        {
            using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("nature24bits.jpg"));
            bitmap.RemovePropertyItem(PropertyTagExifUserComment);
            Assert.Equal(new int[] { PropertyTagChrominanceTable, PropertyTagLuminanceTable }, bitmap.PropertyIdList);
            AssertExtensions.Throws <ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagExifUserComment));
            AssertExtensions.Throws <ArgumentException>(null, () => bitmap.RemovePropertyItem(PropertyTagExifUserComment));

            bitmap.RemovePropertyItem(PropertyTagLuminanceTable);
            Assert.Equal(new int[] { PropertyTagChrominanceTable }, bitmap.PropertyIdList);
            AssertExtensions.Throws <ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagLuminanceTable));
            AssertExtensions.Throws <ArgumentException>(null, () => bitmap.RemovePropertyItem(PropertyTagLuminanceTable));

            bitmap.RemovePropertyItem(PropertyTagChrominanceTable);
            Assert.Empty(bitmap.PropertyIdList);
            AssertExtensions.Throws <ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagChrominanceTable));
            Assert.Throws <ExternalException>(() => bitmap.RemovePropertyItem(PropertyTagChrominanceTable));
        }
Beispiel #2
0
 public void FromHandle_IconHandleMultipleTime_Success()
 {
     using (var icon1 = new Icon(Helpers.GetTestBitmapPath("16x16_one_entry_4bit.ico")))
     {
         using (Icon icon2 = Icon.FromHandle(icon1.Handle))
         {
             Assert.Equal(icon1.Handle, icon2.Handle);
             Assert.Equal(icon1.Size, icon2.Size);
             SaveAndCompare(icon2, false);
         }
         using (Icon icon3 = Icon.FromHandle(icon1.Handle))
         {
             Assert.Equal(icon1.Handle, icon3.Handle);
             Assert.Equal(icon1.Size, icon3.Size);
             SaveAndCompare(icon3, false);
         }
     }
 }
Beispiel #3
0
        public static IEnumerable <object[]> ToBitmap_TestData()
        {
            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("16x16_one_entry_4bit.ico")) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("32x32_one_entry_4bit.ico")) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("48x48_one_entry_1bit.ico")) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("64x64_one_entry_8bit.ico")) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("96x96_one_entry_8bit.ico")) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("256x256_two_entries_multiple_bits.ico"), 48, 48) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("256x256_two_entries_multiple_bits.ico"), 256, 256) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("256x256_two_entries_multiple_bits.ico"), 0, 0) });
        }
Beispiel #4
0
        public void Save_ClosedOutputStreamNoIconData()
        {
            using (var source = new Icon(Helpers.GetTestBitmapPath("48x48_multiple_entries_4bit.ico")))
                using (var icon = Icon.FromHandle(source.Handle))
                {
                    var stream = new MemoryStream();
                    stream.Close();

                    if (PlatformDetection.IsNetFramework)
                    {
                        // The ObjectDisposedException is ignored in previous .NET versions,
                        // so the following does nothing.
                        icon.Save(stream);
                    }
                    else
                    {
                        Assert.Throws <ObjectDisposedException>(() => icon.Save(stream));
                    }
                }
        }
Beispiel #5
0
        public void CorrectColorDepthExtracted()
        {
            using (var stream = File.OpenRead(Helpers.GetTestBitmapPath("pngwithheight_icon.ico")))
            {
                using (var icon = new Icon(stream, new Size(32, 32)))
                {
                    // The first 32x32 icon isn't 32 bit. Checking a few pixels that are in the 32 bit entry.
                    using (Bitmap bitmap = icon.ToBitmap())
                    {
                        Assert.Equal(new Size(32, 32), bitmap.Size);

                        int       expectedBitDepth;
                        string    fieldName = PlatformDetection.IsNetFramework ? "bitDepth" : "s_bitDepth";
                        FieldInfo fi        = typeof(Icon).GetField(fieldName, BindingFlags.Static | BindingFlags.NonPublic);
                        expectedBitDepth = (int)fi.GetValue(null);

                        // If the first icon entry was picked, the color would be black: 0xFF000000?

                        switch (expectedBitDepth)
                        {
                        case 32:
                            Assert.Equal(0x879EE532u, (uint)bitmap.GetPixel(0, 0).ToArgb());
                            Assert.Equal(0x661CD8B7u, (uint)bitmap.GetPixel(0, 31).ToArgb());
                            break;

                        case 16:
                        case 8:
                            // There is no 16 bit 32x32 icon in this file, 8 will be picked
                            // as the closest match.
                            Assert.Equal(0x00000000u, (uint)bitmap.GetPixel(0, 0).ToArgb());
                            Assert.Equal(0xFF000000u, (uint)bitmap.GetPixel(0, 31).ToArgb());
                            break;

                        default:
                            Assert.False(true, $"Unexpected bitmap depth: {expectedBitDepth}");
                            break;
                        }
                    }
                }
            }
        }
Beispiel #6
0
        public void ExtractAssociatedIcon_UNCFilePath_Success()
        {
            string bitmapPath     = Helpers.GetTestBitmapPath("48x48_multiple_entries_4bit.ico");
            string bitmapPathRoot = Path.GetPathRoot(bitmapPath);
            string bitmapUncPath  = $"\\\\{Environment.MachineName}\\{bitmapPath.Substring(0, bitmapPathRoot.IndexOf(":"))}$\\{bitmapPath.Replace(bitmapPathRoot, "")}";

            // Some path could not be accessible
            // if so we just pass the test
            try
            {
                File.Open(bitmapUncPath, FileMode.Open, FileAccess.Read, FileShare.Read).Dispose();
            }
            catch (IOException)
            {
                return;
            }

            Assert.True(new Uri(bitmapUncPath).IsUnc);

            ExtractAssociatedIcon_FilePath_Success(bitmapUncPath);
        }
Beispiel #7
0
        public void FromHandle_BitmapHandleMultipleTime_Success()
        {
            IntPtr handle;

            using (var icon1 = new Icon(Helpers.GetTestBitmapPath("16x16_one_entry_4bit.ico")))
            {
                handle = icon1.ToBitmap().GetHicon();
            }
            using (Icon icon2 = Icon.FromHandle(handle))
            {
                Assert.Equal(handle, icon2.Handle);
                Assert.Equal(new Size(16, 16), icon2.Size);
                SaveAndCompare(icon2, false);
            }
            using (Icon icon3 = Icon.FromHandle(handle))
            {
                Assert.Equal(handle, icon3.Handle);
                Assert.Equal(new Size(16, 16), icon3.Size);
                SaveAndCompare(icon3, false);
            }
        }
Beispiel #8
0
        public void PropertyItems_GetBitmapJpg_Success()
        {
            using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("nature24bits.jpg"));
            PropertyItem[] items = bitmap.PropertyItems;
            Assert.Equal(3, items.Length);
            Assert.Equal(PropertyTagExifUserComment, items[0].Id);
            Assert.Equal(29, items[0].Len);
            Assert.Equal(PropertyTagTypeASCII, items[0].Type);
            Assert.Equal("LEAD Technologies Inc. V1.01\0", Encoding.ASCII.GetString(items[0].Value));
            Assert.Equal(PropertyTagChrominanceTable, items[1].Id);
            Assert.Equal(128, items[1].Len);
            Assert.Equal(PropertyTagTypeShort, items[1].Type);
            Assert.Equal(new byte[]
            {
                0x16, 0x00, 0x17, 0x00, 0x1F, 0x00, 0x3E, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x17,
                0x00, 0x1B, 0x00, 0x22, 0x00, 0x57, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x1F,
                0x00, 0x22, 0x00, 0x49, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x3E,
                0x00, 0x57, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00
            }, items[1].Value);
            Assert.Equal(PropertyTagLuminanceTable, items[2].Id);
            Assert.Equal(128, items[2].Len);
            Assert.Equal(PropertyTagTypeShort, items[2].Type);
            Assert.Equal(new byte[]
            {
                0x15, 0x00, 0x0E, 0x00, 0x0D, 0x00, 0x15, 0x00, 0x1F, 0x00, 0x34, 0x00, 0x43, 0x00, 0x50, 0x00, 0x0F,
                0x00, 0x0F, 0x00, 0x12, 0x00, 0x19, 0x00, 0x22, 0x00, 0x4C, 0x00, 0x4F, 0x00, 0x48, 0x00, 0x12,
                0x00, 0x11, 0x00, 0x15, 0x00, 0x1F, 0x00, 0x34, 0x00, 0x4B, 0x00, 0x5B, 0x00, 0x49, 0x00, 0x12,
                0x00, 0x16, 0x00, 0x1D, 0x00, 0x26, 0x00, 0x43, 0x00, 0x72, 0x00, 0x69, 0x00, 0x51, 0x00, 0x17,
                0x00, 0x1D, 0x00, 0x30, 0x00, 0x49, 0x00, 0x59, 0x00, 0x8F, 0x00, 0x87, 0x00, 0x65, 0x00, 0x1F,
                0x00, 0x2E, 0x00, 0x48, 0x00, 0x54, 0x00, 0x6A, 0x00, 0x89, 0x00, 0x95, 0x00, 0x79, 0x00, 0x40,
                0x00, 0x54, 0x00, 0x66, 0x00, 0x72, 0x00, 0x87, 0x00, 0x9F, 0x00, 0x9E, 0x00, 0x85, 0x00, 0x5F,
                0x00, 0x79, 0x00, 0x7D, 0x00, 0x81, 0x00, 0x93, 0x00, 0x84, 0x00, 0x87, 0x00, 0x82, 0x00,
            }, items[2].Value);

            Assert.NotSame(items, bitmap.PropertyItems);
        }
Beispiel #9
0
        public static IEnumerable <object[]> ToBitmap_TestData()
        {
            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("16x16_one_entry_4bit.ico")) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("32x32_one_entry_4bit.ico")) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("48x48_one_entry_1bit.ico")) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("64x64_one_entry_8bit.ico")) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("96x96_one_entry_8bit.ico")) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("256x256_two_entries_multiple_bits.ico"), 48, 48) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("256x256_two_entries_multiple_bits.ico"), 256, 256) });

            yield return(new object[] { new Icon(Helpers.GetTestBitmapPath("256x256_two_entries_multiple_bits.ico"), 0, 0) });

            // Handle rerring to icon without any colour.
            var icon_48x48_one_entry_1bit = new Icon(Helpers.GetTestBitmapPath("48x48_one_entry_1bit.ico"));

            yield return(new object[] { Icon.FromHandle(icon_48x48_one_entry_1bit.Handle) });
        }
Beispiel #10
0
 public void ExtractAssociatedIcon_FilePath_Success()
 {
     ExtractAssociatedIcon_FilePath_Success(Helpers.GetTestBitmapPath("48x48_multiple_entries_4bit.ico"));
 }
Beispiel #11
0
        public static IEnumerable <object[]> Ctor_Bitmap_TestData()
        {
            yield return(new object[] { new Bitmap(10, 10), PixelFormat.Format32bppPArgb, new Size(10, 10) });

            yield return(new object[] { new Metafile(Helpers.GetTestBitmapPath("telescope_01.wmf")), PixelFormat.Format32bppArgb, new Size(490, 654) });
        }
Beispiel #12
0
        public void StopAnimate_Succeeds_ForAnimatedImages_WithNothingAnimating()
        {
            var image = new Bitmap(Helpers.GetTestBitmapPath("animated-timer-100fps-repeat-2.gif"));

            ImageAnimator.StopAnimate(image, (object o, EventArgs e) => { });
        }
Beispiel #13
0
 public void PropertyIdList_GetBitmapJpg_Success()
 {
     using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("nature24bits.jpg"));
     Assert.Equal(new int[] { PropertyTagExifUserComment, PropertyTagChrominanceTable, PropertyTagLuminanceTable }, bitmap.PropertyIdList);
     Assert.NotSame(bitmap.PropertyIdList, bitmap.PropertyIdList);
 }
Beispiel #14
0
 public void RemovePropertyItem_NoSuchPropertyItemEmptyImageBitmapBmp_ThrowsExternalException(int propid)
 {
     using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("almogaver1bit.bmp"));
     Assert.Throws <ExternalException>(() => bitmap.RemovePropertyItem(propid));
 }
Beispiel #15
0
 public void Save_OutputStream_Success(string fileName)
 {
     SaveAndCompare(new Icon(Helpers.GetTestBitmapPath(fileName)), true);
 }
Beispiel #16
0
        public void UpdateFrames_Succeeds_ForNonAnimatedImages_WithNothingAnimating()
        {
            var image = new Bitmap(Helpers.GetTestBitmapPath("1bit.png"));

            ImageAnimator.UpdateFrames(image);
        }
Beispiel #17
0
        public void StopAnimate_Succeeds_ForNonAnimatedImages_WithNothingAnimating()
        {
            var image = new Bitmap(Helpers.GetTestBitmapPath("1bit.png"));

            ImageAnimator.StopAnimate(image, (object o, EventArgs e) => { });
        }
Beispiel #18
0
 public void Unix_OverflowException_CorruptIcon()
 {
     Assert.Throws <OverflowException>(() => new Icon(Helpers.GetTestBitmapPath("overflowicon.ico")));
 }
        public void AnimateAndCaptureFrames()
        {
            // This test animates the test gifs that we have and waits 60 seconds
            // for the animations to progress. As the frame change events occur, we
            // capture snapshots of the current frame, essentially extracting the
            // frames from the GIF.

            // The animation should progress at the expected pace to stay synchronized
            // with the wall clock, and the animated timer images show the time duration
            // within the image itself, so this can be manually verified for accuracy.

            // The captured frames are stored in the `artifacts/bin/System.Drawing.Common.Tests`
            // folder for each configuration, and then under an `ImageAnimatorManualTests` folder
            // with a timestamped folder under that. Each animation image gets its own folder too.

            string[] images = new string[]
            {
                "animated-timer-1fps-repeat-2.gif",
                "animated-timer-1fps-repeat-infinite.gif",
                "animated-timer-10fps-repeat-2.gif",
                "animated-timer-10fps-repeat-infinite.gif",
                "animated-timer-100fps-repeat-2.gif",
                "animated-timer-100fps-repeat-infinite.gif",
            };

            Dictionary <string, EventHandler> handlers     = new();
            Dictionary <string, int>          frameIndexes = new();
            Dictionary <string, Bitmap>       bitmaps      = new();

            Stopwatch stopwatch = new();

            foreach (var imageName in images)
            {
                string testOutputFolder = Path.Combine(OutputFolder, Path.GetFileNameWithoutExtension(imageName));
                Directory.CreateDirectory(testOutputFolder);
                frameIndexes[imageName] = 0;

                handlers[imageName] = new EventHandler(new Action <object, EventArgs>((object o, EventArgs e) =>
                {
                    Bitmap animation = (Bitmap)o;
                    ImageAnimator.UpdateFrames(animation);

                    // We save captures using jpg so that:
                    // a) The images don't get saved as animated gifs again, and just a single frame is saved
                    // b) Saving pngs in this test on Linux was leading to sporadic GDI+ errors; Jpeg is more reliable
                    string timestamp = stopwatch.ElapsedMilliseconds.ToString("000000");
                    animation.Save(Path.Combine(testOutputFolder, $"{++frameIndexes[imageName]}_{timestamp}.jpg"), ImageFormat.Jpeg);
                }));

                bitmaps[imageName] = new(Helpers.GetTestBitmapPath(imageName));
                ImageAnimator.Animate(bitmaps[imageName], handlers[imageName]);
            }

            stopwatch.Start();
            Thread.Sleep(60_000);

            foreach (var imageName in images)
            {
                ImageAnimator.StopAnimate(bitmaps[imageName], handlers[imageName]);
                bitmaps[imageName].Dispose();
            }
        }
Beispiel #20
0
 public void GetPropertyItem_NoSuchPropertyItemEmptyImageBitmapBmp_ThrowsArgumentException(int propid)
 {
     using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("almogaver1bit.bmp"));
     AssertExtensions.Throws <ArgumentException>(null, () => bitmap.GetPropertyItem(propid));
 }
Beispiel #21
0
        public void Save_NullOutputStreamIconData_ThrowsNullReferenceException()
        {
            var icon = new Icon(Helpers.GetTestBitmapPath("48x48_multiple_entries_4bit.ico"));

            Assert.Throws <NullReferenceException>(() => icon.Save(null));
        }
Beispiel #22
0
 public void RemovePropertyItem_NoSuchPropertyNotEmptyBitmapJpg_ThrowsArgumentException(int propid)
 {
     using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("nature24bits.jpg"));
     AssertExtensions.Throws <ArgumentException>(null, () => bitmap.RemovePropertyItem(propid));
 }
Beispiel #23
0
        public void Serialize_RoundtripFromData_Success()
        {
            var icon = new Icon(Helpers.GetTestBitmapPath("48x48_multiple_entries_4bit.ico"));

            Roundtrip(icon);
        }
Beispiel #24
0
        public void SetPropertyItem_InvokeBitmapJpg_Success(int propid)
        {
            using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("nature24bits.jpg"));

            // Change data.
            PropertyItem item = bitmap.GetPropertyItem(PropertyTagExifUserComment);

            item.Value = Encoding.ASCII.GetBytes("Hello World\0");
            item.Len   = item.Value.Length;

            bitmap.SetPropertyItem(item);

            Assert.Equal(new int[] { PropertyTagExifUserComment, PropertyTagChrominanceTable, PropertyTagLuminanceTable }, bitmap.PropertyIdList);
            PropertyItem[] items = bitmap.PropertyItems;
            Assert.Equal(3, items.Length);
            Assert.Equal(PropertyTagExifUserComment, items[0].Id);
            Assert.Equal(12, items[0].Len);
            Assert.Equal(PropertyTagTypeASCII, items[0].Type);
            Assert.Equal("Hello World\0", Encoding.ASCII.GetString(items[0].Value));
            Assert.Equal(PropertyTagChrominanceTable, items[1].Id);
            Assert.Equal(128, items[1].Len);
            Assert.Equal(PropertyTagTypeShort, items[1].Type);
            Assert.Equal(new byte[]
            {
                0x16, 0x00, 0x17, 0x00, 0x1F, 0x00, 0x3E, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x17,
                0x00, 0x1B, 0x00, 0x22, 0x00, 0x57, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x1F,
                0x00, 0x22, 0x00, 0x49, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x3E,
                0x00, 0x57, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00
            }, items[1].Value);
            Assert.Equal(PropertyTagLuminanceTable, items[2].Id);
            Assert.Equal(128, items[2].Len);
            Assert.Equal(PropertyTagTypeShort, items[2].Type);
            Assert.Equal(new byte[]
            {
                0x15, 0x00, 0x0E, 0x00, 0x0D, 0x00, 0x15, 0x00, 0x1F, 0x00, 0x34, 0x00, 0x43, 0x00, 0x50, 0x00, 0x0F,
                0x00, 0x0F, 0x00, 0x12, 0x00, 0x19, 0x00, 0x22, 0x00, 0x4C, 0x00, 0x4F, 0x00, 0x48, 0x00, 0x12,
                0x00, 0x11, 0x00, 0x15, 0x00, 0x1F, 0x00, 0x34, 0x00, 0x4B, 0x00, 0x5B, 0x00, 0x49, 0x00, 0x12,
                0x00, 0x16, 0x00, 0x1D, 0x00, 0x26, 0x00, 0x43, 0x00, 0x72, 0x00, 0x69, 0x00, 0x51, 0x00, 0x17,
                0x00, 0x1D, 0x00, 0x30, 0x00, 0x49, 0x00, 0x59, 0x00, 0x8F, 0x00, 0x87, 0x00, 0x65, 0x00, 0x1F,
                0x00, 0x2E, 0x00, 0x48, 0x00, 0x54, 0x00, 0x6A, 0x00, 0x89, 0x00, 0x95, 0x00, 0x79, 0x00, 0x40,
                0x00, 0x54, 0x00, 0x66, 0x00, 0x72, 0x00, 0x87, 0x00, 0x9F, 0x00, 0x9E, 0x00, 0x85, 0x00, 0x5F,
                0x00, 0x79, 0x00, 0x7D, 0x00, 0x81, 0x00, 0x93, 0x00, 0x84, 0x00, 0x87, 0x00, 0x82, 0x00,
            }, items[2].Value);

            // New data.
            item.Id    = propid;
            item.Value = Encoding.ASCII.GetBytes("New Value\0");
            item.Len   = item.Value.Length;

            bitmap.SetPropertyItem(item);

            Assert.Equal(new int[] { PropertyTagExifUserComment, PropertyTagChrominanceTable, PropertyTagLuminanceTable, propid }, bitmap.PropertyIdList);
            items = bitmap.PropertyItems;
            Assert.Equal(4, items.Length);
            Assert.Equal(PropertyTagExifUserComment, items[0].Id);
            Assert.Equal(12, items[0].Len);
            Assert.Equal(PropertyTagTypeASCII, items[0].Type);
            Assert.Equal("Hello World\0", Encoding.ASCII.GetString(items[0].Value));
            Assert.Equal(PropertyTagChrominanceTable, items[1].Id);
            Assert.Equal(128, items[1].Len);
            Assert.Equal(PropertyTagTypeShort, items[1].Type);
            Assert.Equal(new byte[]
            {
                0x16, 0x00, 0x17, 0x00, 0x1F, 0x00, 0x3E, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x17,
                0x00, 0x1B, 0x00, 0x22, 0x00, 0x57, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x1F,
                0x00, 0x22, 0x00, 0x49, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x3E,
                0x00, 0x57, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00
            }, items[1].Value);
            Assert.Equal(PropertyTagLuminanceTable, items[2].Id);
            Assert.Equal(128, items[2].Len);
            Assert.Equal(PropertyTagTypeShort, items[2].Type);
            Assert.Equal(new byte[]
            {
                0x15, 0x00, 0x0E, 0x00, 0x0D, 0x00, 0x15, 0x00, 0x1F, 0x00, 0x34, 0x00, 0x43, 0x00, 0x50, 0x00, 0x0F,
                0x00, 0x0F, 0x00, 0x12, 0x00, 0x19, 0x00, 0x22, 0x00, 0x4C, 0x00, 0x4F, 0x00, 0x48, 0x00, 0x12,
                0x00, 0x11, 0x00, 0x15, 0x00, 0x1F, 0x00, 0x34, 0x00, 0x4B, 0x00, 0x5B, 0x00, 0x49, 0x00, 0x12,
                0x00, 0x16, 0x00, 0x1D, 0x00, 0x26, 0x00, 0x43, 0x00, 0x72, 0x00, 0x69, 0x00, 0x51, 0x00, 0x17,
                0x00, 0x1D, 0x00, 0x30, 0x00, 0x49, 0x00, 0x59, 0x00, 0x8F, 0x00, 0x87, 0x00, 0x65, 0x00, 0x1F,
                0x00, 0x2E, 0x00, 0x48, 0x00, 0x54, 0x00, 0x6A, 0x00, 0x89, 0x00, 0x95, 0x00, 0x79, 0x00, 0x40,
                0x00, 0x54, 0x00, 0x66, 0x00, 0x72, 0x00, 0x87, 0x00, 0x9F, 0x00, 0x9E, 0x00, 0x85, 0x00, 0x5F,
                0x00, 0x79, 0x00, 0x7D, 0x00, 0x81, 0x00, 0x93, 0x00, 0x84, 0x00, 0x87, 0x00, 0x82, 0x00,
            }, items[2].Value);
            Assert.Equal(propid, items[3].Id);
            Assert.Equal(10, items[3].Len);
            Assert.Equal(PropertyTagTypeASCII, items[3].Type);
            Assert.Equal("New Value\0", Encoding.ASCII.GetString(items[3].Value));

            // Set same.
            bitmap.SetPropertyItem(item);

            Assert.Equal(new int[] { PropertyTagExifUserComment, PropertyTagChrominanceTable, PropertyTagLuminanceTable, propid }, bitmap.PropertyIdList);
            items = bitmap.PropertyItems;
            Assert.Equal(4, items.Length);
            Assert.Equal(PropertyTagExifUserComment, items[0].Id);
            Assert.Equal(12, items[0].Len);
            Assert.Equal(PropertyTagTypeASCII, items[0].Type);
            Assert.Equal("Hello World\0", Encoding.ASCII.GetString(items[0].Value));
            Assert.Equal(PropertyTagChrominanceTable, items[1].Id);
            Assert.Equal(128, items[1].Len);
            Assert.Equal(PropertyTagTypeShort, items[1].Type);
            Assert.Equal(new byte[]
            {
                0x16, 0x00, 0x17, 0x00, 0x1F, 0x00, 0x3E, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x17,
                0x00, 0x1B, 0x00, 0x22, 0x00, 0x57, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x1F,
                0x00, 0x22, 0x00, 0x49, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x3E,
                0x00, 0x57, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82,
                0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00, 0x82, 0x00
            }, items[1].Value);
            Assert.Equal(PropertyTagLuminanceTable, items[2].Id);
            Assert.Equal(128, items[2].Len);
            Assert.Equal(PropertyTagTypeShort, items[2].Type);
            Assert.Equal(new byte[]
            {
                0x15, 0x00, 0x0E, 0x00, 0x0D, 0x00, 0x15, 0x00, 0x1F, 0x00, 0x34, 0x00, 0x43, 0x00, 0x50, 0x00, 0x0F,
                0x00, 0x0F, 0x00, 0x12, 0x00, 0x19, 0x00, 0x22, 0x00, 0x4C, 0x00, 0x4F, 0x00, 0x48, 0x00, 0x12,
                0x00, 0x11, 0x00, 0x15, 0x00, 0x1F, 0x00, 0x34, 0x00, 0x4B, 0x00, 0x5B, 0x00, 0x49, 0x00, 0x12,
                0x00, 0x16, 0x00, 0x1D, 0x00, 0x26, 0x00, 0x43, 0x00, 0x72, 0x00, 0x69, 0x00, 0x51, 0x00, 0x17,
                0x00, 0x1D, 0x00, 0x30, 0x00, 0x49, 0x00, 0x59, 0x00, 0x8F, 0x00, 0x87, 0x00, 0x65, 0x00, 0x1F,
                0x00, 0x2E, 0x00, 0x48, 0x00, 0x54, 0x00, 0x6A, 0x00, 0x89, 0x00, 0x95, 0x00, 0x79, 0x00, 0x40,
                0x00, 0x54, 0x00, 0x66, 0x00, 0x72, 0x00, 0x87, 0x00, 0x9F, 0x00, 0x9E, 0x00, 0x85, 0x00, 0x5F,
                0x00, 0x79, 0x00, 0x7D, 0x00, 0x81, 0x00, 0x93, 0x00, 0x84, 0x00, 0x87, 0x00, 0x82, 0x00,
            }, items[2].Value);
            Assert.Equal(propid, items[3].Id);
            Assert.Equal(10, items[3].Len);
            Assert.Equal(PropertyTagTypeASCII, items[3].Type);
            Assert.Equal("New Value\0", Encoding.ASCII.GetString(items[3].Value));
        }
Beispiel #25
0
        public void CorrectColorDepthExtracted()
        {
            using (var stream = File.OpenRead(Helpers.GetTestBitmapPath("pngwithheight_icon.ico")))
            {
                using (var icon = new Icon(stream, new Size(32, 32)))
                {
                    // The first 32x32 icon isn't 32 bit. Checking a few pixels that are in the 32 bit entry.
                    using (Bitmap bitmap = icon.ToBitmap())
                    {
                        Assert.Equal(new Size(32, 32), bitmap.Size);

                        int expectedBitDepth;
                        if (!PlatformDetection.IsWindows)
                        {
                            // The Unix implementation currently doesn't try to match the display,
                            // it will just pick the highest color depth when creating the bitmap.
                            // (see SaveBestSingleIcon()).
                            expectedBitDepth = 32;
                        }
                        else
                        {
                            string    fieldName = PlatformDetection.IsNetFramework ? "bitDepth" : "s_bitDepth";
                            FieldInfo fi        = typeof(Icon).GetField(fieldName, BindingFlags.Static | BindingFlags.NonPublic);
                            expectedBitDepth = (int)fi.GetValue(null);
                        }

                        // If the first icon entry was picked, the color would be black: 0xFF000000?

                        switch (expectedBitDepth)
                        {
                        case 32:
                            if (!PlatformDetection.IsWindows)
                            {
                                // libgdiplus on Unix doesn't natively support ARGB32 format. It
                                // uses the Cairo library which represents the bitmaps as PARGB32
                                // with individual channels premultiplied with the alpha channel.
                                // When converting back and forth it results in slight loss of
                                // precision so allow both original and rounded values here.
                                uint color = (uint)bitmap.GetPixel(0, 0).ToArgb();
                                Assert.True(color == 0x879EE532u || color == 0x879EE431u, color.ToString("x"));
                                color = (uint)bitmap.GetPixel(0, 31).ToArgb();
                                Assert.True(color == 0x661CD8B7u || color == 0x661BD7B6u, color.ToString("x"));
                            }
                            else
                            {
                                Assert.Equal(0x879EE532u, (uint)bitmap.GetPixel(0, 0).ToArgb());
                                Assert.Equal(0x661CD8B7u, (uint)bitmap.GetPixel(0, 31).ToArgb());
                            }
                            break;

                        case 16:
                        case 8:
                            // There is no 16 bit 32x32 icon in this file, 8 will be picked
                            // as the closest match.
                            Assert.Equal(0x00000000u, (uint)bitmap.GetPixel(0, 0).ToArgb());
                            Assert.Equal(0xFF000000u, (uint)bitmap.GetPixel(0, 31).ToArgb());
                            break;

                        default:
                            Assert.False(true, $"Unexpected bitmap depth: {expectedBitDepth}");
                            break;
                        }
                    }
                }
            }
        }
Beispiel #26
0
 public void PropertyItems_GetEmptyBitmapBmp_Success()
 {
     using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("almogaver1bit.bmp"));
     Assert.Empty(bitmap.PropertyItems);
     Assert.Same(bitmap.PropertyItems, bitmap.PropertyItems);
 }
Beispiel #27
0
        public void UpdateFrames_Succeeds_ForAnimatedImages_WithNothingAnimating()
        {
            var animatedImage = new Bitmap(Helpers.GetTestBitmapPath("animated-timer-100fps-repeat-2.gif"));

            ImageAnimator.UpdateFrames(animatedImage);
        }