Metafile to Bitmap and WindowsXP-SP2

K

Kirk Quinbar

hi,

I have the following sample .NET 1.0 code which converts a metafile to a
bitmap:
Dim tmpImage As Bitmap 'System.Drawing.Image
Dim streamX As New System.IO.MemoryStream()
Dim fs As FileStream, br As BinaryReader
Dim FilePath As String
Dim data() As Byte

FilePath = Application.StartupPath & "\computer.wmf"
streamX = New MemoryStream()
fs = New FileStream(FilePath, FileMode.Open, FileAccess.Read,
FileShare.Read)
br = New BinaryReader(fs)
data = br.ReadBytes(500000)
streamX.Write(data, 0, data.GetLength(0))

tmpImage = New Bitmap(streamX)
Dim outImage As Bitmap = New Bitmap(tmpImage)
outImage.SetResolution(200, 200)
PictureBox1.Image = outImage

I am running under WindowsXP and this code worked perfect before I installed
Service Pack 2 for WindowsXP. Now I get an error in the line tmpImage = New
Bitmap(streamX). The error is "Invalid Parameter Used" and that's it.

Has anyone ever experienced this? I am wondering if this is a new bug
introduced by the Service Pack or did they change something so that WMF
files can't be streamed to a bitmap. I can get this same code to work fine
using a JPG or a GIF, so this error is specific to using a WMF, but in my
situation we'd prefer to use a WMF

Kirk Quinbar
Zywave, Inc.
 
K

Ken Tucker [MVP]

Hi,

Whats wrong with doing it this way?

Dim bm As New Bitmap("Computer.wmf")

bm.Save("computer.bmp", Imaging.ImageFormat.Bmp)



Ken

------------------------------

hi,

I have the following sample .NET 1.0 code which converts a metafile to a
bitmap:
Dim tmpImage As Bitmap 'System.Drawing.Image
Dim streamX As New System.IO.MemoryStream()
Dim fs As FileStream, br As BinaryReader
Dim FilePath As String
Dim data() As Byte

FilePath = Application.StartupPath & "\computer.wmf"
streamX = New MemoryStream()
fs = New FileStream(FilePath, FileMode.Open, FileAccess.Read,
FileShare.Read)
br = New BinaryReader(fs)
data = br.ReadBytes(500000)
streamX.Write(data, 0, data.GetLength(0))

tmpImage = New Bitmap(streamX)
Dim outImage As Bitmap = New Bitmap(tmpImage)
outImage.SetResolution(200, 200)
PictureBox1.Image = outImage

I am running under WindowsXP and this code worked perfect before I installed
Service Pack 2 for WindowsXP. Now I get an error in the line tmpImage = New
Bitmap(streamX). The error is "Invalid Parameter Used" and that's it.

Has anyone ever experienced this? I am wondering if this is a new bug
introduced by the Service Pack or did they change something so that WMF
files can't be streamed to a bitmap. I can get this same code to work fine
using a JPG or a GIF, so this error is specific to using a WMF, but in my
situation we'd prefer to use a WMF

Kirk Quinbar
Zywave, Inc.
 
K

Kirk Quinbar

I do not want to convert a physical wmf to a physical bmp, i was just giving
some example code to show how I am using the wmf stream to bitmap
functionality. In my real code, the source is is not a physical file. It's a
wmf stream from a chart control and then I have to take that stream, convert
it to a bitmap and set an image type control to that bitmap.

thanks for the suggestion though..

kirk
 
K

Kirk Quinbar

alright, i got the answer direct from Microsoft, so I thought I'd share the
solution:

The New constructor for creation of a bitmap from a stream has changed.
Before SP2, creating a new bitmap from a wmf stream would automatically
reset the stream to position 0, but after SP2, it no longer does that.
Supposedly this was changed to allow more functionality in that you could
read from the stream at any position instead of always from the beginning.
This obviously makes it necessary to reset the stream to the beginning if
your wmf is the whole stream.

so in the code example in my first post, you'd need to do this

streamX.Write(data, 0, data.GetLength(0))
streamX.position = 0 'reset the stream to the beginning


That should do it!

Kirk Quinbar
Zywave, Inc.
 

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