Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Icon.ToBitmap() bug?

Icon.ToBitmap() bug?

Scheduled Pinned Locked Moved C#
helpcsharpdotnetgraphicsquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Tsuda Kageyu

    Yesterday, I tried to convert an Icon consists of single PNG image into a Bitmap with Icon.ToBitmap() method. But it threw a mysterious ArgumentOutOfRangeException. I've investigated the cause of the error, and reached a conclusion. The method doesn't assume that the icons may contain PNG images. Is it a bug of .NET framework?

    realJSOPR Offline
    realJSOPR Offline
    realJSOP
    wrote on last edited by
    #2

    No. It's a misunderstanding on your part of what an icon is.

    .45 ACP - because shooting twice is just silly
    -----
    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

    T 1 Reply Last reply
    0
    • realJSOPR realJSOP

      No. It's a misunderstanding on your part of what an icon is.

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      T Offline
      T Offline
      Tsuda Kageyu
      wrote on last edited by
      #3

      Sorry, I can't understand what you said. You mean an icon which contains bitmaps in PNG format is incorrent one?

      H 1 Reply Last reply
      0
      • T Tsuda Kageyu

        Sorry, I can't understand what you said. You mean an icon which contains bitmaps in PNG format is incorrent one?

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #4

        It means that even though the (png) image is being used as an icon, it is not in the Icon image format (*.ico). It is just a bitmap and you do not need to convert it to load it into a bitmap. Treat it exactly the same as you would a *.bmp file.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

        T 1 Reply Last reply
        0
        • T Tsuda Kageyu

          Yesterday, I tried to convert an Icon consists of single PNG image into a Bitmap with Icon.ToBitmap() method. But it threw a mysterious ArgumentOutOfRangeException. I've investigated the cause of the error, and reached a conclusion. The method doesn't assume that the icons may contain PNG images. Is it a bug of .NET framework?

          K Offline
          K Offline
          KarstenK
          wrote on last edited by
          #5

          No. I would call it a "special feature". :-O conclusion: shit happens

          Press F1 for help or google it. Greetings from Germany

          1 Reply Last reply
          0
          • H Henry Minute

            It means that even though the (png) image is being used as an icon, it is not in the Icon image format (*.ico). It is just a bitmap and you do not need to convert it to load it into a bitmap. Treat it exactly the same as you would a *.bmp file.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

            T Offline
            T Offline
            Tsuda Kageyu
            wrote on last edited by
            #6

            mmm... I'm still not convinced. This is head 48 bytes of my .ico file.

            0x0000: 00 00 01 00 01 00 00 00 00 00 01 00 20 00 d2 d9
            0x0010: 00 00 16 00 00 00 89 50 4e 47 0d 0a 1a 0a 00 00
            0x0020: 00 0d 49 48 44 52 00 00 01 00 00 00 01 00 08 06

            This has correct ICONDIR, CONDIRENTRY and PNG image after them. It's not a renamed PNG file. It's a correct icon, isn't it?

            H 1 Reply Last reply
            0
            • T Tsuda Kageyu

              mmm... I'm still not convinced. This is head 48 bytes of my .ico file.

              0x0000: 00 00 01 00 01 00 00 00 00 00 01 00 20 00 d2 d9
              0x0010: 00 00 16 00 00 00 89 50 4e 47 0d 0a 1a 0a 00 00
              0x0020: 00 0d 49 48 44 52 00 00 01 00 00 00 01 00 08 06

              This has correct ICONDIR, CONDIRENTRY and PNG image after them. It's not a renamed PNG file. It's a correct icon, isn't it?

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #7

              Unfortunately, I am not familiar enough with the various image formats to identify that as a 'correct' header or not. It may be that the image was produced by a program that doesn't quite match with what the converter was expecting, and there are loads of those around. You might try loading it into one of the many Icon Editor programs and, if it loads OK, do a Save As, then try your routine again with the copy.

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

              1 Reply Last reply
              0
              • T Tsuda Kageyu

                Yesterday, I tried to convert an Icon consists of single PNG image into a Bitmap with Icon.ToBitmap() method. But it threw a mysterious ArgumentOutOfRangeException. I've investigated the cause of the error, and reached a conclusion. The method doesn't assume that the icons may contain PNG images. Is it a bug of .NET framework?

                T Offline
                T Offline
                The Man from U N C L E
                wrote on last edited by
                #8

                There is most definitely a bug in the .Net framework, as explained by Paulo Santos[^]. Sorry if the link breaks later, but it only seems to have survived in the Google cache. The short of it is that the Icon class calls the Win API function CreateIconFromResourceEx to extract the appropriately sized image from the .ico file with the wrong parameters, resulting in it always loading the closest match to a 32x32 image it can find. Paulo does explain a way to force it to load a 256x256 vista icon image, however he also discovered that .png images incorrectly register their size in the .ico file as 0x0 pixels. So, with only one .png in the .ico file it will always be loaded, but the size recorded in the Icon object will never match. Consequently a subsequent call to Icon.ToBitMap will attempt to re-load the wrong size image from the .ico file and will promptly crash with an ArgumentOutOfRangeException! The solution is, don't use .png images in icons. Or if you do then make sure you also include a 32x32 pixel .bmp image. Alternatively, wait for a bug fix from Microsoft, but don't hold your breath.

                If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

                H T 2 Replies Last reply
                0
                • T The Man from U N C L E

                  There is most definitely a bug in the .Net framework, as explained by Paulo Santos[^]. Sorry if the link breaks later, but it only seems to have survived in the Google cache. The short of it is that the Icon class calls the Win API function CreateIconFromResourceEx to extract the appropriately sized image from the .ico file with the wrong parameters, resulting in it always loading the closest match to a 32x32 image it can find. Paulo does explain a way to force it to load a 256x256 vista icon image, however he also discovered that .png images incorrectly register their size in the .ico file as 0x0 pixels. So, with only one .png in the .ico file it will always be loaded, but the size recorded in the Icon object will never match. Consequently a subsequent call to Icon.ToBitMap will attempt to re-load the wrong size image from the .ico file and will promptly crash with an ArgumentOutOfRangeException! The solution is, don't use .png images in icons. Or if you do then make sure you also include a 32x32 pixel .bmp image. Alternatively, wait for a bug fix from Microsoft, but don't hold your breath.

                  If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #9

                  Excellent answer!

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.

                  1 Reply Last reply
                  0
                  • T The Man from U N C L E

                    There is most definitely a bug in the .Net framework, as explained by Paulo Santos[^]. Sorry if the link breaks later, but it only seems to have survived in the Google cache. The short of it is that the Icon class calls the Win API function CreateIconFromResourceEx to extract the appropriately sized image from the .ico file with the wrong parameters, resulting in it always loading the closest match to a 32x32 image it can find. Paulo does explain a way to force it to load a 256x256 vista icon image, however he also discovered that .png images incorrectly register their size in the .ico file as 0x0 pixels. So, with only one .png in the .ico file it will always be loaded, but the size recorded in the Icon object will never match. Consequently a subsequent call to Icon.ToBitMap will attempt to re-load the wrong size image from the .ico file and will promptly crash with an ArgumentOutOfRangeException! The solution is, don't use .png images in icons. Or if you do then make sure you also include a 32x32 pixel .bmp image. Alternatively, wait for a bug fix from Microsoft, but don't hold your breath.

                    If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

                    T Offline
                    T Offline
                    Tsuda Kageyu
                    wrote on last edited by
                    #10

                    Thank you very much for your detailed answer! In fact, I've already found the workaround like this.

                    Bitmap bmp;
                    using (var ms = new MemoryStream())
                    {
                    icon.Save(ms);
                    bmp = (Bitmap)Image.FromStream(ms);
                    }

                    Now, I'm rewriting my poor article and dirty code. I will put it in my code.

                    T 1 Reply Last reply
                    0
                    • T Tsuda Kageyu

                      Thank you very much for your detailed answer! In fact, I've already found the workaround like this.

                      Bitmap bmp;
                      using (var ms = new MemoryStream())
                      {
                      icon.Save(ms);
                      bmp = (Bitmap)Image.FromStream(ms);
                      }

                      Now, I'm rewriting my poor article and dirty code. I will put it in my code.

                      T Offline
                      T Offline
                      The Man from U N C L E
                      wrote on last edited by
                      #11

                      Your welcome. The Paulo link I posted claimed to have an replacement Icon class, but I couldn't download it. Shame really, as I bet it would have been stuffed with goodies relevant to your article.

                      If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups