Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

imaging.metafile(stream): "generic error" in GDI+ ?

239 views
Skip to first unread message

Tim

unread,
Sep 18, 2002, 11:46:24 AM9/18/02
to
Can someone please tell this graphics programming novice why he's getting
the following error on line 3?
"value cannot be null; parameter name: encoder".

Dim img As New System.Drawing.Bitmap(Application.StartupPath + "\my.bmp")
Dim strm As New System.IO.MemoryStream()
img.Save(strm, System.Drawing.Imaging.ImageFormat.Wmf)
Dim meta As New System.Drawing.Imaging.Metafile(strm)

Thanks
Tim


John Hornick [MS]

unread,
Sep 18, 2002, 12:14:41 PM9/18/02
to
Hi,


You can't "save" a metafile. You "record" a metafile. To do so, create
a metafile with one of the constructors which takes an HDC as a parameter,
then wrap a Graphics around it and draw on the Graphics.

Thanks,
- John
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.
Visit http://www.microsoft.com/security for current information on security.

Tim

unread,
Sep 18, 2002, 12:46:43 PM9/18/02
to
John,
Thanks for the reply. I will describe in more detail what I'm trying to
accomplish, and would be grateful for your advice.

I am working with quasi-characters from ancient manuscripts which as yet
have no corresponding Unicode character, so they are stored as bitmap
images. The application has a Rich Text control (mandatory for other
reasons). I want to embed bitmapsof these quasi-characters into lines of
(styled) text in the Rich Text control.

Based on the "{\pict\wmetafile8..." code in the RTF string that I see when I
save a WordPad document to RTF format and then look at it in a text editor,
I assume one must convert the bitmap into a format whose bytes can be
embedded as a metafile into the RTF property:

{\pict\wmetafile8\picw225\pich194\picwgoal225\pichgoal194
0100090000039b0000000200400000000000050000000b0200000000050000000c0257018c01
09
000000f7000003020000000000ffffff00040000003402000003000000350040000000430f20
00
cc0000000d000f000000000057018c0100000000280000000f0000000d000000010001000000
00
0034000000c40e0000c40e0000000000000000000000000000ffffff00fffe0000fffe0000ff
fe
000080000000c0060000e00e0000f01e0000f83e0000fc7e0000fefe0000fffe0000fffe0000
ff
fe00002d000000f7000003140000000000800000000080000080800000000080008000800000
80
8000c0c0c000c0dcc000a6caf000fffbf000a0a0a40080808000ff00000000ff0000ffff0000
00
00ff00ff00ff0000ffff00ffffff00040000003402010004000000f0010000030000000000
}

What is the most straightforward way of reading the bitmap from disk and
then transferring those raw bytes into the RTF string of the RichText
control? I would like the RichText control to render the embedded image as
part of the styled text without my having to draw the image where I want it
to be placed in the word: "abc[quasi-char]def". Is this possible?
Thanks
Tim Romano


"John Hornick [MS]" <JHor...@online.microsoft.com> wrote in message
news:QaYiZ5yXCHA.1816@cpmsftngxa08...

John Hornick [MS]

unread,
Sep 19, 2002, 1:22:51 PM9/19/02
to
Hi,


I don't know much about the RTF format. If you've gotten it to work, as
above, with other WMFs, then your question becomes, simply, "how do I
put a bitmap into a WMF". You'd do that something like this:

Metafile m = new Metafile( .... hDC );
Graphics g = Graphics.FromImage( m );
Image i = new Bitmap( filename );
g.DrawImage( i, 0, 0 );
g.Dispose();
i.Dispose();
m.Dispose();

Tim

unread,
Sep 19, 2002, 2:57:59 PM9/19/02
to
John,
Thanks for the additional information. It's becoming clearer to me, but I'm
still in the dark about one thing. Now that I have drawn the bitmap image
into the metafile, so to speak, is there a member method of the Metafile
class that returns the raw bytes of the metafile minus the MetaFileHeader? I
have to insert these bytes, in hex representation, into the RTF string.

Dim img As New Bitmap(Application.StartupPath + "\my.bmp")
Dim g As Graphics = CreateGraphics()
Dim hDC As IntPtr = g.GetHdc()
Dim m As System.Drawing.Imaging.Metafile
m = New System.Drawing.Imaging.Metafile(hDC,
System.Drawing.Imaging.EmfType.EmfOnly)
g.ReleaseHdc(hDC)
g.FromImage(m)
g.DrawImage(img, 0, 0)

' here we extract the metafile bytes (excluding the header) and write
them to the RTF string
...

Thanks
Tim

"John Hornick [MS]" <JHor...@online.microsoft.com> wrote in message

news:0FMBMEAYCHA.1944@cpmsftngxa07...

John Hornick [MS]

unread,
Sep 20, 2002, 9:58:22 AM9/20/02
to
Hi,

> Thanks for the additional information. It's becoming clearer to me, but
I'm
> still in the dark about one thing. Now that I have drawn the bitmap image
> into the metafile, so to speak, is there a member method of the Metafile
> class that returns the raw bytes of the metafile minus the
MetaFileHeader? I
> have to insert these bytes, in hex representation, into the RTF string.
>
> Dim img As New Bitmap(Application.StartupPath + "\my.bmp")
> Dim g As Graphics = CreateGraphics()
> Dim hDC As IntPtr = g.GetHdc()
> Dim m As System.Drawing.Imaging.Metafile
> m = New System.Drawing.Imaging.Metafile(hDC,
> System.Drawing.Imaging.EmfType.EmfOnly)
> g.ReleaseHdc(hDC)
> g.FromImage(m)
> g.DrawImage(img, 0, 0)
>
> ' here we extract the metafile bytes (excluding the header) and
write
> them to the RTF string
> ...


Are you sure you want EMF bits, and not WMF bits? Check into that and
ensure you are using the right kind of metafile.

To get the metafile bits, create the metafile on a stream, then once
you are done recording the metafile, Dispose() it and access the Stream
bytes.

0 new messages