Resizing BLOB Image to ImageView

Hi,

This is not a treasure of any kind, but it might still worth to be shared. Here is how I get an image from database which is stored in BLOB, then resized down proportionally to fit in an ImageView:

Public Function ResizePictureToImageView(SourceImage as picture, byref DestHeight as integer, byref DestWidth as integer, byref xPos as integer, byref yPos as integer, FrameHeight as integer, FrameWidth as integer) as Picture
if SourceImage.Height > SourceImage.Width then //Portrait
if SourceImage.Height > FrameHeight then
DestHeight = FrameHeight
DestWidth = DestHeight * (SourceImage.Width / SourceImage.Height)
xPos = 0 + (FrameWidth - DestWidth) / 2
yPos = 0
else
DestHeight = SourceImage.Height
DestWidth = SourceImage.Width
xPos = 0 + (FrameWidth - DestWidth) / 2
yPos = 0 + (FrameHeight - DestHeight) / 2
end if
else // Landscape
if SourceImage.Width > FrameWidth then
DestWidth = FrameWidth
DestHeight = DestWidth * (SourceImage.Height / SourceImage.Width)
xPos = 0
yPos = 0+ (FrameHeight - DestHeight) / 2
else
DestHeight = SourceImage.Height
DestWidth = SourceImage.Width
xPos = 0 + (FrameWidth - DestWidth) / 2
yPos = 0 + (FrameHeight - DestHeight) / 2
end if
end if

dim Resized as New Picture(FrameWidth, FrameHeight)
Resized.graphics.DrawPicture(SourceImage, 0, 0, DestWidth, DestHeight, 0, 0, SourceImage.Width, SourceImage.Height)

return Resized

End Function

I call the function by:

dim rs as RecordSet

dim SourceImage, Resized as Picture
dim DestHeight, DestWidth, xPos, yPos as integer = 0

SourceImage = rs.field(“NameCard1”).PictureValue
if SourceImage <> nil then
Resized = app.ResizePictureToImageView(SourceImage, DestHeight, DestWidth, xPos, yPos, ImageView1.Height, ImageView1.Width)
ImageView1.Picture = Resized
else
ImageView1.Picture = nil
end if

Cool! Thanks for sharing!

Code shared on the forum can be encased in [code] tags so that spacing is maintained. It’s easier to read :slight_smile: