Recent

Author Topic: How to resize a TBgraBitmap?  (Read 4109 times)

hayanninja

  • New Member
  • *
  • Posts: 45
How to resize a TBgraBitmap?
« on: January 27, 2018, 04:49:04 pm »
When I try using TBgraBitmap.Resample, the resulting bitmap is blank.

When I try using TBgraBitmap.CanvasBgra.CopyRect, the result is... very messed up. (I'm going to guess that it expects the source and dest rects to be the same size, and only maybe differ in position?)

So - what's the best way to achieve resizing an image? I'm not really fussed as to whether it can resize the same TBgraBitmap object or if it needs to output the result to a different one; I can work with either way. Likewise, I can work with either "take a region SrcRect from a source image and draw it to a region DstRect on a destination image, where SrcRect and DstRect are of different sizes", or "change the dimensions of a TBgraBitmap object while properly resizing the image it contains to the new dimensions".
« Last Edit: January 27, 2018, 05:02:18 pm by hayanninja »

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: How to resize a TBgraBitmap?
« Reply #1 on: January 27, 2018, 06:05:31 pm »
the below works for me for TBitmap. maybe it helps...

Code: Pascal  [Select][+][-]
  1. procedure ResizeBitmap(var AImage: TBitmap; NewWidth, NewHeight: word);
  2. var
  3.   b1: TBitmap;
  4.   b2: TBitmap;
  5.  
  6. begin
  7.   try
  8.     b1 := TBitmap.Create;
  9.     b2 := TBitmap.Create;
  10.  
  11.     b1.SetSize(AImage.Width, AImage.Height);
  12.     b1.Assign(AImage);
  13.  
  14.     b2.SetSize(AImage.Width, AImage.Height);
  15.     b2.Canvas.FillRect(b2.Canvas.ClipRect);
  16.     b2.Canvas.StretchDraw(Rect(0, 0, NewWidth, NewHeight), b1);
  17.  
  18.     AImage.Width := NewWidth;
  19.     AImage.Height := NewHeight;
  20.  
  21.     AImage.Canvas.Clear;
  22.     AImage.Canvas.Draw(0, 0, b2);
  23.  
  24.   finally
  25.     FreeAndNil(b1);
  26.     FreeAndNil(b2);
  27.   end;
  28. end;
Lazarus 2.0.2 64b on Debian LXDE 10

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: How to resize a TBgraBitmap?
« Reply #2 on: January 27, 2018, 06:58:07 pm »
When I try using TBgraBitmap.Resample, the resulting bitmap is blank.

When I try using TBgraBitmap.CanvasBgra.CopyRect, the result is... very messed up. (I'm going to guess that it expects the source and dest rects to be the same size, and only maybe differ in position?)

So - what's the best way to achieve resizing an image? I'm not really fussed as to whether it can resize the same TBgraBitmap object or if it needs to output the result to a different one; I can work with either way. Likewise, I can work with either "take a region SrcRect from a source image and draw it to a region DstRect on a destination image, where SrcRect and DstRect are of different sizes", or "change the dimensions of a TBgraBitmap object while properly resizing the image it contains to the new dimensions".

Code: Pascal  [Select][+][-]
  1. Bitmap: TBGRABitmap;
  2. ...
  3. BGRAReplace(Bitmap, Bitmap.Resample(100, 50, rmSimpleStretch));
  4. ...

hayanninja

  • New Member
  • *
  • Posts: 45
Re: How to resize a TBgraBitmap?
« Reply #3 on: January 28, 2018, 02:02:30 am »
When I try using TBgraBitmap.Resample, the resulting bitmap is blank.

When I try using TBgraBitmap.CanvasBgra.CopyRect, the result is... very messed up. (I'm going to guess that it expects the source and dest rects to be the same size, and only maybe differ in position?)

So - what's the best way to achieve resizing an image? I'm not really fussed as to whether it can resize the same TBgraBitmap object or if it needs to output the result to a different one; I can work with either way. Likewise, I can work with either "take a region SrcRect from a source image and draw it to a region DstRect on a destination image, where SrcRect and DstRect are of different sizes", or "change the dimensions of a TBgraBitmap object while properly resizing the image it contains to the new dimensions".

Code: Pascal  [Select][+][-]
  1. Bitmap: TBGRABitmap;
  2. ...
  3. BGRAReplace(Bitmap, Bitmap.Resample(100, 50, rmSimpleStretch));
  4. ...

Thanks, I'll try this next time I'm working on my project (later tonight). :)

EDIT: It worked, thanks! :)
« Last Edit: January 28, 2018, 09:06:35 am by hayanninja »

 

TinyPortal © 2005-2018