Jump to content

resize image TUniFileUpload


chrisjohn82

Recommended Posts

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;
Link to comment
Share on other sites

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;
Link to comment
Share on other sites

  • 3 years later...
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...