Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Border to Image (GDI+) 1

Status
Not open for further replies.

nevets2001uk

IS-IT--Management
Jun 26, 2002
609
0
0
GB
Hi,

I have been searching around but have not found out how to attempt this. I'm after a way to dynamically add a 5px black border to an existing image on the server and then save the changes.

I'd also like to be able to add a small caption to the bottom right corner if possible.

I think this should be possible using GDI+ but I can't see how. I use vb for my code.

Regards,
Steve

Steve G (MCP)
 
YOu just create a graphics object the same size (or a bit bigger then your image and then draw the image on that wiht .drawimage and then draw the rectangle with drawrectangle and then the string.

And then use the save method.

What code do you have so far?

Christiaan Baes
Belgium

"My new site" - Me
 
It shouldn't be too difficult to implement this. You'll just have to get the existing dimensions of the first image and then create a slightly larger image and color it black. You can then just position the first image over the top of it.

If you are having problems actually implementing this, post back with what you've got so far and which bit isn't working correctly.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
To be honest I've not got a lot as I'm struggling to use the GDI+ library.

Does the below code look like a sensible start?

filename = Server.MapPath("~/" & fullsizedestination) & strFilename & ".jpg"

Dim framed As System.Drawing.Image = New Bitmap(filename)

Dim objGraphics As System.Drawing.Graphics
objGraphics = System.Drawing.Graphics.FromImage(framed)

'DO STUFF

framed.Save(Server.MapPath("~/" & frameddestination) & strFilename & ".jpg")

Regards,
Steve

Steve G (MCP)
 
something like this should work (untested)

Code:
Imports System.Drawing

Public Class Class2
    Private Sub makeimage(ByVal filename As String)
        Dim framed As System.Drawing.Image = New Bitmap(filename)
        Dim newpicture As New Bitmap(framed.Width + 10, framed.Height + 10)
        Dim objGraphics As System.Drawing.Graphics
        objGraphics = Graphics.FromImage(newpicture)
        objGraphics.DrawImage(framed, 5, 5)
        objGraphics.DrawRectangle(Pens.Black, 0, 0, newpicture.Width, newpicture.Height)
        objGraphics.DrawString("text to write", New Font("Arial", 12, FontStyle.Regular), Brushes.Black, 10, 10)
        framed.Dispose()
        framed = Nothing
        newpicture.Save(filename)
    End Sub
End Class


Christiaan Baes
Belgium

"My new site" - Me
 
Thanks very much. I've not tried it yet but that code makes sense to me. Much more so than the examples I found on the web! I'll let you know how it goes.

Having had a slight change of heart as to how I'm implementing this framing I was thinking of instead of saving the image to the server to instead apply the frame and then display it directly in the browser. This would then leave the original image untouched and a range of framing styles to be used. I've seen this line used in tutorials...

objBitmap.Save( Response.OutputStream, ImageFormat.Gif )

but I'm not sure how I 'position' the image on the page using that method. Is it like response.write where the text just appears at the top of the page? I'd like to be able to use a <img> tag or something on the page so that I can define where the image will appear.

Any thoughts on how to do that and whether it will work with in this situation?

Steve G (MCP)
 
Have managed to work this out myself. Should have played before asking!

The final code I used for the framing was...

Code:
Dim framed As System.Drawing.Image = New Bitmap(filename)
        Dim newpicture As New Bitmap(framed.Width + 20, framed.Height + 20)
        Dim objGraphics As System.Drawing.Graphics
        objGraphics = Graphics.FromImage(newpicture)
        objGraphics.FillRectangle(New SolidBrush(Color.Black), 0, 0, newpicture.Width, newpicture.Height)
        objGraphics.FillRectangle(New SolidBrush(Color.White), 5, 5, newpicture.Width - 10, newpicture.Height - 10)
        objGraphics.DrawImage(framed, 10, 10, framed.Width, framed.Height)
        'UNITS PARAMETER TO CHANGE UNITS TO PIXELS?
        objGraphics.DrawString("Copyright © 2006", New Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.Gray, 10, 10)
        framed.Dispose()
        framed = Nothing

        newpicture.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)

I had to change the drawrectangle to fillrectangle but other than that it worked straightaway.

Thanks very much for the help.

Steve G (MCP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top