1 (edited by mathmathou 2016-10-23 06:31:03)

Topic: [Script] - Short guide to resizing images

Here is a quick tip for resizing images with MVD


This works with jpeg, but you can easily adapt the code for whatever format you want. Just keep in mind that, for what I known now, if working with png you will loose the transparancy with this method.


The code is commented, just drop that on a form with a button, correct the images paths and names (or better add an openfiledialog)

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
var
    bmp : TBitmap;
    jpg : TJpegImage;
    scale : Double;
begin
    try
        //-----> Create the jpeg
        jpg := TJpegImage.Create;
            try
                //-----> assign a file to the jpeg
                jpg.LoadFromFile('.\test.jpg');
                    //-----> if/else to find out which dimension is the larger and set size ratio - I chose 450 but
                    //you cna choose whatever you want
                    if jpg.Height > jpg.Width then scale := 450 / jpg.Width else scale := 450 / jpg.Height;
                    //create the bitmap image
                    bmp := TBitmap.Create;
                        try
                            //-----> define the size of the bitmap
                            bmp.Width := Round(jpg.Width  * scale);
                            bmp.Height := Round(jpg.Height * scale);
                            //-----> resize the jpg by loading it into the bitmap with new dimensions
                            bmp.Canvas.StretchDraw(0,0,bmp.Width,bmp.Height, jpg);
                            //-----> reassign the bitmpa to the jpeg
                            jpg.Assign(bmp);
                            //-----> save the file
                            jpg.SaveToFile('.\test_thumb.jpg');
                        except
                            ShowMessage(ExceptionMessage);
                        end;
            except
                ShowMessage(ExceptionMessage);
            end;
    finally
        //Free all images
        bmp.Free;
        jpg.Free;
    end;
end;

Have fun all, this is what is really important after all !!


Cheers



Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: [Script] - Short guide to resizing images

Perfect solution! I take it in my piggy bank. It's a pity that in MVDB there is no full-fledged support for working with PNG format, so that transparency is not lost.

Визуальное программирование: блог и телеграм-канал.

Re: [Script] - Short guide to resizing images

I adapt the images to the color of my form so it looks like the background is transparent.

Destiny

Re: [Script] - Short guide to resizing images

Destiny wrote:

I adapt the images to the color of my form so it looks like the background is transparent.

That's right, it works well if the required transparency is 100%. But I was spoiled by free libraries with images of buttons with translucent elements, in PNG format. They look very good due to anti-aliasing, which is achieved by smoothly changing the transparency of the edge of the image.

I'm currently working on responsive button images that will change color depending on the UI style chosen (active and passive button colors). Unfortunately, I will have to switch to the BMP format - only with it in MVDB it is possible to implement what I had in mind.

Визуальное программирование: блог и телеграм-канал.

Re: [Script] - Short guide to resizing images

Да, это верно для бесплатных изображений. Я модифицирую их с помощью программного обеспечения для редактирования, смягчая края, это требует времени, но результат идеален, поэтому я сохраняю формат PNG.

Destiny