ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Blurring "outside area" when pasting image
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

HeartWare

Denmark
50 Posts

Posted - Mar 01 2022 :  03:27:09  Show Profile  Reply
If I have one TBitMap of 100x150 and one TBitMap of 100x100 and I want this 100x100 bitmap centered in the middle of the 100x150 bitmap, but with the 100x25 areas on top and bottom be a "blurred" extension of the pasted image (like you can do with TImageEnView BackgroundStyle=iebsBlurredImage when the image doesn't fill the entire ImageView area) - how do I programmatically do this?

The code should also handle the situation where the blurring is on the sides, as well as on all four sides...

xequte

38608 Posts

Posted - Mar 01 2022 :  16:21:44  Show Profile  Reply
Yes, you can do it as follows:

// CREATE BITMAP WITH BLURRED IMAGE FILLING NON-IMAGE AREA
// DestBmp: Output image for result
// BackgroundBmp: Image to use as background (must be valid)
// ImageBmp: Image to draw centered (can be same as BackgroundBmp, or can be NIL)
// Width, Height: Size to make DestBmp
// BlurRadius: How much to blur image (Default is 8, 0 for no blur)
// BackgroundOpacity: If less than 1.0, the background is drawn semi-opaque (with BackgroundColor visible)
procedure StretchDrawWithBlur(DestBmp, BackgroundBmp, ImageBmp: TIEBitmap; 
                              Width, Height: Integer; 
                              BlurRadius: Integer = 8; 
                              BackgroundOpacity: Double = 0.75; BackgroundColor: TColor = clBlack);
var
  aRect: TRect;
  DestRect : TIERectangle;
begin
  // Allocate and size
  if not assigned( DestBmp ) then
    DestBmp := TIEBitmap.create;
  DestBmp.Allocate( Width, Height );

  // Fill background color if needed
  if BackgroundOpacity < 1.0 then
    DestBmp.Fill( BackgroundColor );

  // Scale draw the background to fill entire image area
  aRect := GetImageRectWithinArea( BackgroundBmp.Width, BackgroundBmp.Height, DestBmp.Width, DestBmp.Height,
                                   0, 0, True, True, True, True, 0,
                                   _fmFillRect_WithOverlap );
  DestRect := IERectangle( aRect );
  BackgroundBmp.RenderToTIEBitmapEx( DestBmp, DestRect.X, DestRect.Y, DestRect.Width, DestRect.Height,
                                     0, 0, BackgroundBmp.Width, BackgroundBmp.Height,
                                     True, 255, rfNone, ielNormal, BackgroundOpacity );

  // Blur the background
  // IEGBlur() is the same as calling  TImageEnProc.Blur()
  if BlurRadius > 0 then
    IEGBlur( DestBmp, BlurRadius, nil, nil );

  // Draw our image
  if ImageBmp <> nil then
  begin
    aRect := GetImageRectWithinArea( ImageBmp.Width, ImageBmp.Height, DestBmp.Width, DestBmp.Height );
    DestRect := IERectangle( aRect );
    ImageBmp.RenderToTIEBitmapEx( DestBmp, DestRect.X, DestRect.Y, DestRect.Width, DestRect.Height,
                                  0, 0, ImageBmp.Width, ImageBmp.Height,
                                  True, 255, rfNone, ielNormal, 1.0 );
  end;
end;


// EXAMPLE USAGE
procedure TForm1.Button1Click(Sender: TObject);
var
  bmpIn, bmpOut: TIEBitmap;
begin
  bmpIn := TIEBitmap.Create;
  bmpOut := TIEBitmap.Create();
  try
    bmpIn.Read( 'D:\im.jpg' );
    StretchDrawWithBlur( bmpOut, bmpIn, bmpIn, 800, 600, 8, 0.75, clWhite );

    ImageEnView1.Assign( bmpOut );
  finally
    bmpIn.Free;
    bmpOut.Free;
  end;
end;




Nigel
Xequte Software
www.imageen.com
Go to Top of Page

HeartWare

Denmark
50 Posts

Posted - Mar 02 2022 :  01:23:18  Show Profile  Reply
Excellent. Will try it out immediately.

Thank you...
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: