Microsoft KB Archive/814735

= Degradation occurs when you try to serialize and deserialize 32-bit icons =

Article ID: 814735

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * 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:  Add a TreeView control to Form1, and then add an ImageList control to Form1. Add a 32-bit icon to the ImageList1 control.

Note Make sure you set the ImageSize property to 32 ,32. Bind ImageList1 to the ImageList property of the TreeView control in the Properties window.  Replace the code in the Button1_Click event handler with the following code: Try TreeView1.ImageList = ImageList1 Dim oStream As Stream = File.Create(&quot;D:\Test1.dat&quot;) Dim oSerializer As BinaryFormatter = New BinaryFormatter oSerializer.Serialize(oStream, TreeView1.ImageList.ImageStream) oStream.Close Catch ex As Exception MessageBox.Show(ex.Message) End Try   Replace the code in the Button2_Click event handler with the following code: Try Dim oStream As Stream = File.OpenRead(&quot;D:\Test1.dat&quot;) 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(&quot;Test &quot; & 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.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
 Start Microsoft Visual Studio .NET.</li> Create a new Windows Application project by using Microsoft Visual Basic .NET.

By Default, Form 1 is created.</li> Drag a PictureBox control to Form1, and then drag two Button controls to Form1.</li> On the View menu, click Code to display the Code window.</li>  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 </li>  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(&quot;C:\Test.ico&quot;) Dim oStream As Stream = File.Create(&quot;C:\Test.dat&quot;) Dim serializer As BinaryFormatter = New BinaryFormatter htIcons.Add(&quot;C:\Test.ico&quot;, 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(&quot;C:\Test.dat&quot;) Dim deserializer As BinaryFormatter = New BinaryFormatter Dim oCache As Hashtable = CType(deserializer.Deserialize(oStream), Hashtable) oStream.Close PictureBox1.Image = oCache.Item(&quot;C:\Test.ico&quot;).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. </li> On the Debug menu, click Start to run the application.</li> 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.</li> 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 &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">