Results 1 to 5 of 5

Thread: Enlarging a bitmap

  1. #1
    brainleak is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    27
    Reputation
    7

    Enlarging a bitmap

    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?

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,813
    Reputation
    1707
    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.

  3. #3
    brainleak is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    27
    Reputation
    7
    Quote Originally Posted by jmcilhinney View Post
    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.

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,813
    Reputation
    1707
    Quote Originally Posted by brainleak View Post
    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.

  5. #5
    brainleak is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    27
    Reputation
    7
    Great, thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •