Enlarging a bitmap

brainleak

Well-known member
Joined
Dec 20, 2016
Messages
54
Location
Barcelona area
Programming Experience
3-5
I have a large number of old printed photographs which I have been scanning. I've written short descriptions in the file names but I've thought I'd like to place the text on the pictures themselves. Not covering the image but on a narrow white strip I would add at the bottom.

I have the photos in bitmaps. So how do I enlarge the bitmaps keeping the same width and adding a narrow white (or any other color) strip at the bottom?
 
You would create a new Bitmap of the desired size and then draw the old Bitmap onto the new. The area that you don't draw onto will be white by default and you can then draw your text onto it. E.g.
Private Function AddTextToBitmap(bmp As Bitmap, text As String) As Bitmap
    Dim newBmp As New Bitmap(bmp.Width, bmp.Height + 20)

    Using g = Graphics.FromImage(newBmp)
        g.DrawImage(bmp, Point.Empty)
        g.DrawString(text, Me.Font, Brushes.Black, 10, bmp.Height + 10)
    End Using

    Return newBmp
End Function

You'll probably want to take a look at the overloads of DrawString to see what options you have. I'm guessing that you'll want to use the one that takes a Rectangle as the area to draw in and draw in the middle-centre of that.
 
You would create a new Bitmap of the desired size and then draw the old Bitmap onto the new. The area that you don't draw onto will be white by default and you can then draw your text onto it. E.g.
Private Function AddTextToBitmap(bmp As Bitmap, text As String) As Bitmap
    Dim newBmp As New Bitmap(bmp.Width, bmp.Height + 20)

    Using g = Graphics.FromImage(newBmp)
        g.DrawImage(bmp, Point.Empty)
        g.DrawString(text, Me.Font, Brushes.Black, 10, bmp.Height + 10)
    End Using

    Return newBmp
End Function

You'll probably want to take a look at the overloads of DrawString to see what options you have. I'm guessing that you'll want to use the one that takes a Rectangle as the area to draw in and draw in the middle-centre of that.
Thanks a lot, looks simple enough. And you're right, the text in the middle of a rectangle is my first option.
 
Thanks a lot, looks simple enough. And you're right, the text in the middle of a rectangle is my first option.

In that case, you'd probably want to specify the entirety of the new area as the Rectangle:
Private Function AddTextToBitmap(bmp As Bitmap, text As String) As Bitmap
    Dim newBmp As New Bitmap(bmp.Width, bmp.Height + 20)

    Using g = Graphics.FromImage(newBmp)
        g.DrawImage(bmp, Point.Empty)

        Dim rect As New Rectangle(0, bmp.Height, bmp.Width, newBmp.Height - bmp.Height)
        Dim fmt As New StringFormat With {.Alignment = StringAlignment.Center,
                                          .LineAlignment = StringAlignment.Center}


        g.DrawString(text, Me.Font, Brushes.Black, rect, fmt)
    End Using

    Return newBmp
End Function

You'll probably want to specify the font a bit differently than using the form font.
 
Back
Top