locked
Add a special border to image. RRS feed

  • Question

  • User944339287 posted

    Hi guys... i can add yellow border to image by using the following code.

        Public Function AppendBorder(ByVal original As System.Drawing.Image, ByVal borderWidth As Integer) As System.Drawing.Image
    
            Dim borderColor As Color = Color.Yellow
            Dim mypen As New Pen(borderColor, borderWidth * 2)
            Dim totalCanvasSize As New System.Drawing.Size(512, 512)
    
            Dim newImageRectWithoutBorder As New System.Drawing.Rectangle(borderWidth, borderWidth, totalCanvasSize.Width - (borderWidth * 2), totalCanvasSize.Height - (borderWidth * 5))
    
            Dim img As New System.Drawing.Bitmap(totalCanvasSize.Width, totalCanvasSize.Height)
            Dim g As Graphics = Graphics.FromImage(img)
    
            'g.Clear(borderColor)   
            g.DrawImage(original, newImageRectWithoutBorder)
            g.DrawRectangle(mypen, 0, 0, totalCanvasSize.Width, totalCanvasSize.Height)
            g.Dispose()
    
            Return img
    
        End Function

    1) i wonder how can i make the white area displaying yellow color as well?
    I knew it's because of i've set borderWidth * 5 (the reason why is because i gonna show extra space for text at the bottom part)


     
    2) Secondly, is it possible to show background image instead of color?

    Monday, April 22, 2019 7:28 AM

All replies

  • User-1174608757 posted

    Hi kengkit,

    According to your description, I have made two samples here.

    1.I have tried your  code,and I think you want to remove the extra white space. Here is my code:

    Imports System.Drawing
    Imports System.Drawing.Imaging
    
    Public Class file
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("d11.jpg"))
            Dim s1 As System.Drawing.Image = AppendBorder(img, 10, 10)
            s1.Save(Server.MapPath("d55.jpg"))
        End Sub
        Public Function AppendBorder(ByVal img As System.Drawing.Image, ByVal hmargin As Integer, ByVal vmargin As Integer) As System.Drawing.Image
            Dim width As Integer = img.Width
            Dim height As Integer = img.Height
            Dim dpiX As Single = img.HorizontalResolution
            Dim dpiY As Single = img.VerticalResolution
            Dim map As Bitmap = New Bitmap(width + 2 * hmargin, height + vmargin * 2, PixelFormat.Format24bppRgb)
            map.SetResolution(dpiX, dpiY)
            Dim grp As Graphics = Graphics.FromImage(map)
            Dim Rec As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 0, width + 2 * hmargin, height + vmargin * 2)
            Dim mySolidBrush As SolidBrush = New SolidBrush(System.Drawing.Color.Yellow)
            grp.FillRectangle(mySolidBrush, Rec)
            grp.DrawImage(img, hmargin, vmargin, Rec, GraphicsUnit.Pixel)
            grp.Dispose()
            Return map
        End Function
    End Class

    image shows as below:

    Then if you want to set an image as a background for another img, you could firstly get a picture and set it as a drawing board then you could set the image you want to add it above the background image, you could set its position and size. Here is the code , I hope it could help you.

    Imports System.Drawing
    Imports System.Drawing.Imaging
    
    Public Class file
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim i As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("d10.jpg"))'picture is 300*450
            Dim i1 As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("d11.jpg"))'picture is 300*450
    
            Using g As Graphics = Graphics.FromImage(i)'i is background
                g.DrawImage(i1, 50, 75, 200, 300) 'set i1 on i,50 represents distance from left ,75 represents distance from height,200 represents width ,300 represents height
                i.Save(Server.MapPath("d13.jpg"))
            End Using
        End Sub
    
    End Class

    You could see 

    Best Regards

    Wei

    Tuesday, April 23, 2019 6:47 AM
  • User944339287 posted

    Hi, thanks for your reply.

    May i know how can i add extra space to the bottom part with filled by yellow color? (top, left and right remain the same space)

    Pls refer to my image, [Fantasy Sky] on the white area is the extra space i have successfully added to the bottom part of the image.. but the color is transparency (white)

    How can i make it a yellow color as well? TQ

    Sunday, April 28, 2019 12:59 AM