Degradation occurs when you try to serialize and deserialize 32-bit icons (814735)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1

SYMPTOMS

When you try to serialize 32-bit icons to a file, you try to deserialize the icons from that file, and then you try to display the icons, there may be degradation in the icons. The colors are not displayed correctly or there is a corrupted background behind the icons.

WORKAROUND

To work around this problem, add the icons to an ImageList control, and then directly serialize the ImageList control. To do this, follow these steps:
  1. Add a TreeView control to Form1, and then add an ImageList control to Form1.
  2. Add a 32-bit icon to the ImageList1 control.

    Note Make sure you set the ImageSize property to 32 ,32.
  3. Bind ImageList1 to the ImageList property of the TreeView control in the Properties window.
  4. Replace the code in the Button1_Click event handler with the following code:
    Try
                TreeView1.ImageList = ImageList1
                Dim oStream As Stream = File.Create("D:\Test1.dat")
                Dim oSerializer As BinaryFormatter = New BinaryFormatter
                oSerializer.Serialize(oStream, TreeView1.ImageList.ImageStream)
                oStream.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
  5. Replace the code in the Button2_Click event handler with the following code:
    Try
                Dim oStream As Stream = File.OpenRead("D:\Test1.dat")
                Dim oDeSerializer As BinaryFormatter = New BinaryFormatter
                Dim Il As ImageList = New ImageList
                Il.ImageStream = CType(oDeSerializer.Deserialize(oStream), ImageListStreamer)
                ImageList1 = Il
                oStream.Close()
                TreeView1.ImageList = ImageList1
                TreeView1.Nodes.Clear()
                Dim count as Integer
                For count = 0 To ImageList1.Images.Count - 1
                    Dim n As TreeNode = New TreeNode("Test " & count, count, count)
                    TreeView1.Nodes.Add(n)
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior


  1. Start Microsoft Visual Studio .NET.
  2. Create a new Windows Application project by using Microsoft Visual Basic .NET.

    By Default, Form 1 is created.
  3. Drag a PictureBox control to Form1, and then drag two Button controls to Form1.
  4. On the View menu, click Code to display the Code window.
  5. Add the following code to the top of the Form1.vb file:
    Imports System
    Imports System.Runtime.Serialization.Formatters.Binary
    Imports System.Collections
    Imports System.Drawing
    Imports System.IO
    Imports System.Runtime.InteropServices
  6. Append the following code in the Form1 class:
    Protected htIcons As Hashtable = New Hashtable
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                'Change the path of the Icon based on the location of the Icon on your computer.
                Dim oIcon As Icon = New Icon("C:\Test.ico")
                Dim oStream As Stream = File.Create("C:\Test.dat")
                Dim serializer As BinaryFormatter = New BinaryFormatter
                htIcons.Add("C:\Test.ico", oIcon)
                serializer.Serialize(oStream, htIcons)
                oStream.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Try
                Dim oStream As Stream = File.OpenRead("C:\Test.dat")
                Dim deserializer As BinaryFormatter = New BinaryFormatter
                Dim oCache As Hashtable = CType(deserializer.Deserialize(oStream), Hashtable)
                oStream.Close()
                PictureBox1.Image = oCache.Item("C:\Test.ico").ToBitmap
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
    Note If there is no Test.ico file in the C:\ directory, or if you have not copied an .ico file that is named Test.ico to the C:\ directory, then change the C:\Test.dat path in this code to a valid path that points to a 32-bit .ico file.
  7. On the Debug menu, click Start to run the application.
  8. On Form1, click Button1 to serialize the icon to the Test.dat file.

    The Test.dat file is located in the \bin folder, under the application folder.
  9. On Form1, click Button2 to deserialize the icon from the file, and to display the icon in the PictureBox control.

    The icon is corrupted as mentioned in the "Symptoms" section of this article.

REFERENCES

For more information about how to implement serialization by using the BinaryFormatter class, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:1/24/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbvs2002sp1sweep kberrmsg kbWindowsForms kbbug KB814735 kbAudDeveloper