[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Loading System.Drawing.Icon from MemoryStream giving exception

Posted on 2007-10-16
12
Medium Priority
?
944 Views
Last Modified: 2013-11-07
I am using .NET 1.1 with VB.NET.  I am attempting to save and load an icon from the database.  I have successfully saved the icon to the database (at least I think so) by using the icon class' Save method to write the icon to a stream.  I then convert the stream to a byte array and pass that in to a parameter to a sql command.  No problem there.  But when i try to reload the icon, i get a byte array from the database, assign that to a stream and attempt to use the constructor that takes a stream.

Me.tmpImage = Image.FromStream(strm) 'works just fine.
Me.tmpIcon = New Icon(strm) 'fails

To create the icon, I used Axialis Icon Studio.  

Let me know if you need more info.
0
Comment
Question by:dougschultz
  • 7
  • 5
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20085045
Where are you loading the byte array into the MemoryStream for the Icon constructor?

Bob
0
 

Author Comment

by:dougschultz
ID: 20086186
I query the database for a table and then:
Dim strm As New MemoryStream(CType(dtImages.Rows(0)("Icon"), Byte()))
0
 

Author Comment

by:dougschultz
ID: 20086199
By the way, this column is an image type.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:dougschultz
ID: 20087231
Does no one have a clue on this one?

Thank you...
0
 

Author Comment

by:dougschultz
ID: 20087497
well i guess i will try to move forward creating a temporary file to hold the icon, although i am loath to do this...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20095218
Sorry, I got busy.  It seems like a problem with how it is written to the database, or read from the database, and I can't see a problem yet.

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20095234
You might be able to take the byte array and write that to a binary file using a BinaryWriter, and then compare that file with the source .ico file, with something like a Hex editor to see if there are binary differences between the two.

Bob
0
 

Author Comment

by:dougschultz
ID: 20096007
Actually, the approach I have followed is to write the stored icon to a temp file before using it.  Something like this:
 Public Function GetIcon() As Icon
        Dim tmpIconFileName As String = IO.Path.GetTempFileName()
        Dim tmpIconFileStream As IO.Stream = File.OpenWrite(tmpIconFileName)
        Dim dtImages As DataTable = modDBA.GetDataTable_Generic("SELECT * from tblImages")
        If Not (IsDBNull(dtImages.Rows(0)("Icon"))) Then
            tmpIconFileStream.Write(CType(dtImages.Rows(0)("PrivateLabelIcon"), Byte()), 0, CType(dtImages.Rows(0)("PrivateLabelIcon"), Byte()).Length)
            tmpIconFileStream.Close()
            Return New Icon(tmpIconFileName)
        End If
    End Function
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20096043
And, did that work?

Bob
0
 

Author Comment

by:dougschultz
ID: 20096075
Oh, heh.  Yes it did work, it works grandly.  I really don't like using a temp file, but it works.  Thanks for your help!
... how do i assign these points?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 20096113
Try this first, as a test:

     Public Function GetIcon() As Icon
         Dim dtImages As DataTable = modDBA.GetDataTable_Generic("SELECT * from tblImages")
        If dtImages.Rows.Count > 0 AndAlso Not IsDBNull(dtImages.Rows(0)("Icon")) Then
            Dim stream As New IO.MemoryStream(dtImages.Rows(0)("Icon"))
            Return New Icon(stream)
        End If
    End Function

Bob
0
 

Author Comment

by:dougschultz
ID: 20096227
What the...  That works fine.  Which is strange because its exactly what I was having problems with before..  Thanks!
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question