Resize pictures and icons

Hi guys, in my program I have inserted many icons on the buttons … the icons appear in a list inside the program in the Picture folder.
I wanted to know if there is the possibility to resize them from there or do I have to do them one at a time. Thanks.

Yes, you can; in the documentation, chack FrawPicture

Syntax is:
DrawPicture(image As Picture, x As Double, y As Double, destWidth As Double = -10000.0, destHeight As Double = -10000.0, sourceX As Double = 0.0, sourceY As Double = 0.0, sourceWidth As Double = -10000.0, sourceHeight As Double = -10000.0)

There are some examples.

Not tested code:



// In the Action event of your button:

Var openDialog As New OpenFileDialog
openDialog.Filter = "Images (*.png, *.jpg, *.jpeg)|*.png;*.jpg;*.jpeg"

Var file As FolderItem = openDialog.ShowModal

If file <> Nil Then
    Var originalImage As Picture = Picture.Open(file)
    If originalImage <> Nil Then
        Var targetWidth As Integer = 150
        Var aspectRatio As Double = originalImage.Width / originalImage.Height
        Var newHeight As Integer = Round(targetWidth / aspectRatio)
        Var resizedImage As Picture = originalImage.ScaleTo(targetWidth, newHeight)

        // Assuming you have an ImageView control named 'ResizedImageView'
        ResizedImageView.Image = resizedImage
    Else
        MsgBox("Could not open the selected image.")
    End If
Else
    // User cancelled the dialog
End If

Not sure what you are after … If you want to resize the pictures for good you have to use an external graphics app to do the resizing and re-import the edited pictures – under macOS I’m using GraphicsConverter for batch-resizing pictures. If however you want to resize the pictures at runtime you can use the DrawPicture method as suggested by Emile. It’s very flexible and this way you can use one picture in various places at different sizes.

2 Likes

Keep in mind that each image should be at least two to three times the largest size visible in the program. This way, you’ll be prepared for scaled screens and won’t lose any quality by enlarging an image beyond its actual size.

1 Like

If you’re talking about so-called image sets, you’ll need to create them again. If you’re talking about image files like PNG, you can simply replace them.

for menu icons i repaint it to smaller size, the method get the image name in project.

MenuExit.Icon = MenuIcon(CloseWindow)

Public Function MenuIcon(pic As Picture) As Picture
  Var picMenu As New Picture(24.0, 24.0)
  Var g As Graphics = picMenu.Graphics
  g.DrawPicture(pic, 0.0, 0.0, g.Width, g.Height, 0.0, 0.0, pic.Width, pic.Height)
  
  Return picMenu
End Function

Hi, I think that with this function I can adapt all the icons to the various buttons, putting the resizing at the Open of the program. Only I can’t understand how DrawPicture works, I read the guide but it seems complicated to understand (maybe by translating I can’t understand well how it works).
From documentation:
DrawPicture
image As Picture, x As Double, y As Double, destWidth As Double = -10000.0, destHeight As Double = -10000.0, sourceX As Double = 0.0, sourceY As Double = 0.0, sourceWidth As Double = -10000.0, sourceHeight As Double = -10000.0

Basically you tell DrawPicture where the pixels you want to draw are located within the source picture and where to draw these pixels in the destination graphics. If the size of the source area differs from the size of the target area then DrawPicture will scale the picture accordingly.

image As Picture, a reference of the image to resize

x As Double, y As Double, Where to draw it in the destination (Canvas)

destWidth As Double = -10000.0, Resize it to Width
destHeight As Double = -10000.0, Resize it to Heigh

sourceX As Double = 0.0, x value of the Source
sourceY As Double = 0.0, y value of the Source

sourceWidth As Double = -10000.0, Width of the p<icture to resize
sourceHeight As Double = -10000.0 Height of the picture to resize

The four last parameters gives the area of the image to resize

it get the image size (rectangle) from picture graphics and the target rectangle (it will stretched/shrink).
you will find other examples if you need to take care the aspect ratio.