System.InvalidCastException for Bitmap Constructor -- Please help!!!!

M

Me

Hello,

I am getting a System.InvalidCastException from this line of code : myBitmap
= New Bitmap("myfile.bmp", True).

I am working on a Win2K, .NET 2003, VB.NET environment. I am developing for
the .NET Compact Framework using an iPAQ 4350. In order to isoloate the
problem, I always run my code on .NET Framework Desktop system.

Below is the entire code listing. The code works perfectly on a .NET 2003
,Windows 2K Desktop PC. Running the identical code on a .NET 2003 Pocket PC
(iPAQ 4350) yields the System.InvalidCastException exception.

I have confirmed that the file is acutally created on the PDA.
I have noticed one other thing. When I run the code on the Desktop, the
bitmap image displays perfectly in the PictureBox. However, if I copy the
..bmp file that my code created on the Desktop to the PDA, I can open the
file, but nothing appears to be displayed.

Also Note that the original files is a JPEG which I convert to a bitmap.

Dim Response As WebResponse
Dim ResponseStream As Stream
Dim Request As WebRequest

Request = WebRequest.Create("http://www.images.com/picture.jpg")
Response = Request.GetResponse
ResponseStream = Response.GetResponseStream

Dim numBytesToRead As Integer = CInt(Response.ContentLength)
Dim bytes(Response.ContentLength) As Byte
Dim numBytesRead As Integer = 0
While numBytesToRead + 1 > 0
Dim n As Integer = ResponseStream.Read(bytes, numBytesRead,
numBytesToRead)
If n = 0 Then
Exit While
End If
numBytesRead += n
numBytesToRead -= n
End While

Dim b As Bitmap
b = New Bitmap(New MemoryStream(bytes))

Dim fs As New FileStream("myfile.bmp", FileMode.Create)

fs.Write(bytes, 0, bytes.Length)
fs.Close()
'Clean up before exiting.
Cursor.Current = Cursors.Default
Response.Close()

Dim myBitmap As Bitmap

'This error showed up on in the the following line of code (ONLY in
..NET CF).
'An unhandled exception of type 'System.InvalidCastException'
occurred in Microsoft.VisualBasic.dll
'Additional(Information) : InvalidCastException()

--> myBitmap = New Bitmap("myfile.bmp", True)

' The image loads nicely from the file, but only from .Net Framework,
NOT
..NET CF!
PictureBox1.Image = myBitmap
PictureBox1.Update()

Any input on what is going on, why, and how I might solve the problem?

Thanks,

Gigi
 
M

Me

I have verified that I'm using ActiveSync V 3.7.1 (Build 3244).

I think that I have .NET CF 1.0 SP2, but how can I very this?

Gigi
 
M

Me

I have verified that I have .NET CF 1.0 SP2 installed. The way that I did
this was the following:

Use File Explorer to navigate to the "\Windows" directory and click the file
called CGACUTIL. You will get a message box showing you the Win32 File
Version of the Compact Framework installed on the device. If SP2 is
installed the version number you see should be [1.0.3316.0] or greater.

Gigi
 
M

Me

I solved the problems:

1. myBitmap = New Bitmap("myfile.bmp", True) was incorrect. I changed this
line to myBitmap = New Bitmap("myfile.bmp") which got rid of the error and
displayed the photo in my control. But that was only half the problem.

2. I changed "myfile.bmp" to myfile.jpeg. Obviously, it didn't make much
difference on the Desktop but it made a world of difference on the PDA.
Once I did that, I was able to create an image file on the desktop and open
it up in the PDA.

Gigi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top