473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

overlay text on uploaded picture

My web site accepts uploaded photos and stores them in a SQL table as BLObs
so they never touch the filesystem.

I have a way to create a thumbnail version of the uploaded image and store
that in the db too, again without touching metal.

I need a way to overlay some text on the image but I'm stuck.

All the help I find uses files in the filesystem, and when I try to convert
the example to my imagestream model it fails and I can't figure out why. I'm
not big on graphics, so I need some help.

Here's the (working) code I use to get the uploaded image down to a
thumbnail size...
Private Function createThumbnail(ByVal ImageStream As Stream, ByVal tWidth
As Double, ByVal tHeight As Double) As Byte()
Dim g As System.Drawing.Image
Dim thumbSize As Size
Dim imgOutput As Bitmap
Dim imgStream As MemoryStream
Dim thisFormat As Object
Dim imgbin() As Byte
Dim n As Int32
Try
g = System.Drawing.Image.FromStream(ImageStream)
thumbSize = NewthumbSize(g.Width, g.Height, tWidth, tHeight)
imgOutput = New Bitmap(g, thumbSize.Width, thumbSize.Height)
imgStream = New MemoryStream
thisFormat = g.RawFormat
imgOutput.Save(imgStream, thisFormat)
ReDim imgbin(imgStream.Length)
imgStream.Position = 0
n = imgStream.Read(imgbin, 0, imgbin.Length)
Catch ex As Exception
Throw ex
Finally
g.Dispose()
imgOutput.Dispose()
End Try
Return imgbin
End Function

And here's the (non-working) code I need fixing to get text onto the
original image.

Private Function createProof(ByVal ImageStream As Stream) As Byte()
Dim image As System.Drawing.Image
Dim graphic As System.Drawing.Graphics
Dim imgOutput As Bitmap
Dim imgStream As MemoryStream
Dim thisFormat As Object
Dim imgbin() As Byte
Dim n As Int32
Dim myBrush As Drawing2D.HatchBrush
Dim myFont As Font
Try
image = System.Drawing.Image.FromStream(ImageStream)
myBrush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.Trellis,
Color.FromArgb(127, Color.White))
myFont = New Font("Arial black", 144)
graphic = Graphics.FromImage(image)
graphic.DrawString("PROOF", myFont, myBrush, New RectangleF(10, 10, 100,
200))
imgOutput = New Bitmap(image.Width, image.Height, graphic)
imgStream = New MemoryStream
imgOutput.Save(imgStream, image.RawFormat)
ReDim imgbin(imgStream.Length)
imgStream.Position = 0
n = imgStream.Read(imgbin, 0, imgbin.Length)
Catch ex As Exception
Throw ex
Finally
graphic.Dispose()
imgOutput.Dispose()
End Try
Return imgbin
End Function

Help me out someone, please...

Brian Lowe
---------@
Nov 18 '05 #1
5 4851
In article <u7**************@tk2msftngp13.phx.gbl>, no@reply.address
says...
My web site accepts uploaded photos and stores them in a SQL table as BLObs
so they never touch the filesystem.

I have a way to create a thumbnail version of the uploaded image and store
that in the db too, again without touching metal.

I need a way to overlay some text on the image but I'm stuck.


Just a guess, but I would try using the Graphics.FromImage() method to
get a Graphics object and then you should be able to do your overlay
with that.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 18 '05 #2
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
Just a guess, but I would try using the Graphics.FromImage() method to
get a Graphics object and then you should be able to do your overlay
with that.


Thanks. I figured I needed a Graphics object, and also that I'd need the
FromImage() method to get my image into a form I could lay text on.

Here's my code so far...

' I have my image data as a System.IO.Stream but
' I need a System.Drawing.Graphics object
' Graphics won't convert a stream but System.Drawing.Image
' will and Graphics will convert an Image so...
image = System.Drawing.Image.FromStream(ImageStream)
graphic = Graphics.FromImage(image)
' Now I have my image data as a Graphics object
' Set up tools for writing text...
myBrush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.Trellis,
Color.FromArgb(127, Color.White))
myFont = New Font("Arial black", 144)
' Use the DrawString() method of the graphics object to
' overlay the text (tell me if I'm wrong, please)
graphic.DrawString("PROOF", myFont, myBrush, New RectangleF(10, 10, 100,
200))
' I can convert a Stream to a byte array and that's what
' I need to output. I can convert a Bitmap into a Stream, so...
' Create a Bitmap of the correct width and
' hieght using my Grpahics object image
imgOutput = New Bitmap(image.Width, image.Height, graphic)
' Prepare a new Stream
imgStream = New MemoryStream
' Send the Bitmap down the Stream
imgOutput.Save(imgStream, image.RawFormat)
' Prepare the Byte array to take the stream data
ReDim imgbin(imgStream.Length)
' Set the pointer to the start of the stream
imgStream.Position = 0
' Stream the Bitmap data into the Byte array
n = imgStream.Read(imgbin, 0, imgbin.Length)

That's the plan, anyway.

I end up with an image the right size but all black.

I don't see how I can even debug this stuff - there's nothing I can look at
between steps to show me where my graphic is up to.

I'm stuck.

Brian Lowe
---------@


Nov 18 '05 #3
In article <eS*************@TK2MSFTNGP12.phx.gbl>, no@reply.address
says...
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
Just a guess, but I would try using the Graphics.FromImage() method to
get a Graphics object and then you should be able to do your overlay
with that.


Thanks. I figured I needed a Graphics object, and also that I'd need the
FromImage() method to get my image into a form I could lay text on.

Here's my code so far...

' I have my image data as a System.IO.Stream but
' I need a System.Drawing.Graphics object
' Graphics won't convert a stream but System.Drawing.Image
' will and Graphics will convert an Image so...
image = System.Drawing.Image.FromStream(ImageStream)
graphic = Graphics.FromImage(image)
' Now I have my image data as a Graphics object
' Set up tools for writing text...
myBrush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.Trellis,
Color.FromArgb(127, Color.White))
myFont = New Font("Arial black", 144)
' Use the DrawString() method of the graphics object to
' overlay the text (tell me if I'm wrong, please)
graphic.DrawString("PROOF", myFont, myBrush, New RectangleF(10, 10, 100,
200))


At this point, the bitmap pointed to by "image" should have the text on
it and you can directly save the "image" object. No need to create a
second bitmap.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 18 '05 #4

"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <eS*************@TK2MSFTNGP12.phx.gbl>, no@reply.address
' Use the DrawString() method of the graphics object to
' overlay the text (tell me if I'm wrong, please)
graphic.DrawString("PROOF", myFont, myBrush, New RectangleF(10, 10, 100,
200))

At this point, the bitmap pointed to by "image" should have the text on
it and you can directly save the "image" object. No need to create a
second bitmap.


Really? I thought .net always used ByVal by default and not ByRef. That
would mean that the image is being modfied as a side effect of methods on a
graphic object created from it. Is that right?

If it is then all I need to do is stream my original (but changed) image
back to a Byte array and I'll be able to store it in SQl as before.

I'll be very happy.

I'll let you know how I get on. Thanks for the help.
The reason I was creating a second bitmap is that I need the image in the
form of a Byte array (so I can then load it into a SQL binary field).

I couldn't go direct from a Graphics object to a Byte array (can I?) so I
loaded the graphic into a Bitmap and then saved it to a Stream and converted
the Stream into a Byte array.

I know its a kludge, and I'm sure there's a very neat and streamlined way to
do what I want, but my limited knowledge of the tools means I need help.

' I can convert a Stream to a byte array and that's what
' I need to output. I can convert a Bitmap into a Stream, so...
' Create a Bitmap of the correct width and
' height using my Graphics object image
imgOutput = New Bitmap(image.Width, image.Height, graphic)
' Prepare a new Stream
imgStream = New MemoryStream
' Send the Bitmap down the Stream
imgOutput.Save(imgStream, image.RawFormat)
' Prepare the Byte array to take the stream data
ReDim imgbin(imgStream.Length)
' Set the pointer to the start of the stream
imgStream.Position = 0
' Stream the Bitmap data into the Byte array
n = imgStream.Read(imgbin, 0, imgbin.Length)

Brian Lowe
---------@
Nov 18 '05 #5
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <eS*************@TK2MSFTNGP12.phx.gbl>, no@reply.address
says...
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om... image = System.Drawing.Image.FromStream(ImageStream)
graphic = Graphics.FromImage(image)
' Now I have my image data as a Graphics object
' Set up tools for writing text...
myBrush = New Drawing2D.HatchBrush(Drawing2D.HatchStyle.Trellis,
Color.FromArgb(127, Color.White))
myFont = New Font("Arial black", 144)
' Use the DrawString() method of the graphics object to
' overlay the text (tell me if I'm wrong, please)
graphic.DrawString("PROOF", myFont, myBrush, New RectangleF(10, 10, 100,
200))


At this point, the bitmap pointed to by "image" should have the text on
it and you can directly save the "image" object. No need to create a
second bitmap.


I understood the default to be ByVal when passing parameters into functions,
so I'd expect graphic to be a copy of image, so anything I do to it would
not change image. Am I wrong?

Obviously I am/was.

I tried saving image and lo! there was my text overlaid on the picture.

Thanks!

Next job is to work out how to size my text so it adequately covers enough
of the picture to be worth stamping "PROOF" on it. Not too small as to
appear only in a corner, and not too large as to darw outside of the image
area.

Thanks for your help.

Brian Lowe
---------@
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gilad Barkan | last post by:
Dear Colleagues, My task: I read AVI file and get handle to its frames using the Ray Mercer's outstanding tutorial regarding video in VB. Now, I try to write text on each such frame and then...
4
by: Yuk Cheng | last post by:
<<<start index.htm>>> <html> <head> <script> function perform(action){ } </script> </head>
5
by: Fabri | last post by:
Someone call please tell me why this simple code: ================================================================= <html> <head> <title>Example</title> <style type="text/css"> #msg{...
9
by: Ross | last post by:
it seems that it is only supported by w3c but not IE or netscape, but what is a w3c browser ?
0
by: Never Best | last post by:
I'm having troubles attaching the Device to a window using its hWnd, and I have some troubles with getting transparancy to work with a DDOverlay... if anyone could help it would be greatlay...
1
by: sendhil | last post by:
hi, Iam doing some graphics programming with C#. I have to create a Windows MetaFile from a base64 encoded string. How do i create the file. I want to Overlay this Windows MetaFile on a image...
3
by: kaczmar2 | last post by:
Hey there, I have a large image in a browser window, and I would like a way to overlay grid lines on top of the image, so a user can show the grid or hide the grid lines. The grid would cover...
5
by: pravinnweb | last post by:
i have dropdown select menu in my page. when i click here link the black overlay comes .in FF overlay working fine but in IE6 the select option menu appeared on overlay...please help me thanks in...
1
by: linksterman | last post by:
I want to make a simple app to overlay subtitles on youtube videos. Is there a way to do this and keep the files hosted on youtubes servers (eg just overlay the text with another flash app)? I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.