Jump to content

Recommended Posts

Posted

Hi everybody,

I want to resize an image if the width is greater than 1000 pix and if it is i want to resize it. I'm using fileupload and loading the file to an imagecontrol and then i'm checking if the width is greater than 1000 pix, so far so good but i can't seem to find a resize function for the imagecontrol? Code below shows want i'm trying to do

 

   imgtest.LoadFromStream(AStream);
   if imgtest.Picture.Width > 1000 then begin
//    ShowMessage('bigger than 1000');
      
      I want to change the size before i save it to destName
 
      imgtest.Picture.SaveToFile(destName);
   
   end;
Posted

I solved it by using to image controls. If the width of the picture is greater than the max allowed width i change the width to the max allowed with and make the height proportional to the new width and then i save the picture with the new 

width and height. See code below 

 

Begin

  J:=TJPEGImage.Create;
      w:=imgtest.Picture.Width;
      h:=imgtest.Picture.Height;
        if w > UniMainModule.picWidth then begin
          p:=round((UniMainModule.picWidth*100)/w);
          w:=UniMainModule.picWidth;
          h:=round((p*h)/100);
       end;
   // set new widht and height in imgtest2 control
      imgtest2.Picture.BITMAP.Width:=w;
      imgtest2.Picture.BITMAP.Height:=h;
      imgtest2.Picture.BITMAP.Canvas.StretchDraw(Rect(0,0,w,h),imgtest.Picture.Graphic);
      J.Assign(imgtest2.Picture.Bitmap);
      J.CompressionQuality:=75;
      J.SaveToFile(destName);
      J.Free;
      imgtest.Picture:=nil;
      fn:=AStream.FileName;
      AStream.Free;
      DeleteFile(fn);
end;
  • 3 years later...
Posted
On 10/17/2016 at 12:20 PM, chrisjohn82 said:

I solved it by using to image controls. If the width of the picture is greater than the max allowed width i change the width to the max allowed with and make the height proportional to the new width and then i save the picture with the new 

width and height. See code below 

 

Begin

  J:=TJPEGImage.Create;
      w:=imgtest.Picture.Width;
      h:=imgtest.Picture.Height;
        if w > UniMainModule.picWidth then begin
          p:=round((UniMainModule.picWidth*100)/w);
          w:=UniMainModule.picWidth;
          h:=round((p*h)/100);
       end;
   // set new widht and height in imgtest2 control
      imgtest2.Picture.BITMAP.Width:=w;
      imgtest2.Picture.BITMAP.Height:=h;
      imgtest2.Picture.BITMAP.Canvas.StretchDraw(Rect(0,0,w,h),imgtest.Picture.Graphic);
      J.Assign(imgtest2.Picture.Bitmap);
      J.CompressionQuality:=75;
      J.SaveToFile(destName);
      J.Free;
      imgtest.Picture:=nil;
      fn:=AStream.FileName;
      AStream.Free;
      DeleteFile(fn);
end;

Good night, how do I activate this picWidth property ??

I can't find her here.

If you have the complete code for this function, it would help me a lot.

Thanks.

Posted
12 hours ago, Sherzod said:

Hello,

Can you please explain in more detail what you wanted?

For example, I am making a system that will need to attach images and save to the PostgreSQL database.

However, customers will have no control over the size of the images.
You will have images with 1mb, 2, 3, 5, 10mb and of different sizes, 1080x980, 700x600, 4096x2048, etc.

So before saving these images in the bank, I would like to convert them to a fixed size: 800x600, which would be about 200kb, so I would save space in the bank and when I request the image on the server, it opens more quickly.

  • 5 years later...
Posted

Does it possible to change images (jpg) sizes on client begore uploading? StretchDraw AFAIR does not work in the Linux systems.

We have issue that users sometime uploaded huge (50 megapixel+) jpg images and it's hard to process it. We need to reduce images sizes before uploading

  • Administrators
Posted

You can also use FMXLinux library and its graphics functions.

Below code worked for me.

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIRegClasses, uniGUIForm,
  uniGUIBaseClasses, uniButton, uniFileUpload, uniImage,
  ServerModule;

type
  TMainForm = class(TUniForm)
    UniButton1: TUniButton;
    UniFileUpload1: TUniFileUpload;
    UniImage1: TUniImage;
    procedure UniButton1Click(Sender: TObject);
    procedure UniFileUpload1Completed(Sender: TObject; AStream: TFileStream);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function MainForm: TMainForm;

implementation

{$R *.dfm}

uses
  uniGUIVars, MainModule, uniGUIApplication,
  FMX.Graphics;

function MainForm: TMainForm;
begin
  Result := TMainForm(UniMainModule.GetFormInstance(TMainForm));
end;

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  UniFileUpload1.Execute;
end;

procedure TMainForm.UniFileUpload1Completed(Sender: TObject;
  AStream: TFileStream);
var
  B : FMX.Graphics.TBitmap;
  sTmp : string;
begin
  B := FMX.Graphics.TBitmap.Create;
  try
    B.LoadFromStream(AStream);
    B.Resize(128, 128);

    sTmp := UniServerModule.NewCacheFile(ExtractFileExt(AStream.FileName));
    B.SaveToFile(sTmp);

    UniImage1.Picture.LoadFromFile(sTmp);
  finally
    B.Free;
  end;

end;

initialization
  RegisterAppFormClass(TMainForm);

end.

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...