469,964 Members | 1,754 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

converting string to image..??

Hi,

I'm trying to convert this simple string into image:

Dim bytes() as byte()=System.text.Encoding.ascii.GetBytes("123")
Dim memStream as System.IO.MemoryStream
Dim img as image
memStream.Write(bytes,0.bytes.length)
img=image.fromstream(memStream) ' an error occurs here
vb.net returns an error: "invlaid parameter" for passing stream to the
image.

I don't want to read stream from file, as I am interested in reading
from input string (textbox).

MTIA,
Grawsha

Jun 30 '06 #1
3 6059

<gr*********@yahoo.com> wrote in message
news:11**********************@y41g2000cwy.googlegr oups.com...
Hi,

I'm trying to convert this simple string into image:

Dim bytes() as byte()=System.text.Encoding.ascii.GetBytes("123")
Dim memStream as System.IO.MemoryStream
Dim img as image
memStream.Write(bytes,0.bytes.length)
img=image.fromstream(memStream) ' an error occurs here
vb.net returns an error: "invlaid parameter" for passing stream to the
image.

I don't want to read stream from file, as I am interested in reading
from input string (textbox).

MTIA,
Grawsha


Ok, from what I understand, you want to write a string onto a image?

Dim s As String = "123"
Dim bmp As Bitmap = New Bitmap(1, 1)
Dim canvas As Graphics = Graphics.FromImage(bmp)
Dim size As Size
Try
' Measure the string.
size = canvas.MeasureString(s, New Font("Verdana", 12))
Finally
canvas.Dispose()
bmp.Dispose()
End Try

bmp = New Bitmap(size.Width, size.Height)
canvas = Graphics.FromImage(bmp)

Try
canvas.DrawString(s, New Font("Verdana", 12))
Finally
canvas.Dispose()
End Try

' Now bmp contains the valid string.

HTH (untested code, btw, may have a few errors, but should work with minor
fixes).
Mythran


Jun 30 '06 #2
well for one....i dont even know what this is making

Dim bytes() as byte()=System.text.Encoding.ascii.GetBytes("123")

try making it either

Dim bytes as byte()=System.text.Encoding.ascii.GetBytes("123")

or

Dim bytes() as byte=System.text.Encoding.ascii.GetBytes("123")
and see how that works
--
-iwdu15
Jun 30 '06 #3

"iwdu15" <jmmgoalsteratyahoodotcom> wrote in message
news:D2**********************************@microsof t.com...
well for one....i dont even know what this is making

Dim bytes() as byte()=System.text.Encoding.ascii.GetBytes("123")

try making it either

Dim bytes as byte()=System.text.Encoding.ascii.GetBytes("123")

or

Dim bytes() as byte=System.text.Encoding.ascii.GetBytes("123")
and see how that works
--
-iwdu15


His problem is that he is trying to load an image from a memory stream that
only contains the bytes of text. The FromStream method expects a byte-array
containing the bytes of an image, not the bytes of a simple string.

By loading the byte-array into an image (FromStream method) he is getting an
exception. So, to do this correctly, he needs to write the string onto an
image using the Graphics class.

HTH :)

Mythran

Jun 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Richard | last post: by
4 posts views Thread by Hal Vaughan | last post: by
reply views Thread by Mark Allen | last post: by
1 post views Thread by retheeshnewage | last post: by
5 posts views Thread by SMichal | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.