Board index » delphi » Keep getting Blank (White) image instead of resized bitmap

Keep getting Blank (White) image instead of resized bitmap

I tried number of variations of bitmap resizing I found from efg and this
newsgroup.
I keep getting the same problem, the resized image comes up white, any ideas
of what I
am doing wrong?
TIA

function ResizeBMP(const BitmapOriginal:TBitMap;
NuWidth,NuHeight:integer):TBitMap;
var
  BItmapNew     :  TBitmap;
begin
    BitmapNew := TBitmap.Create;
    try
      BitmapNew.Width  := NuWidth;
      BitmapNew.Height := NuHeight;
      BitmapNew.PixelFormat := pf24bit;

      // Shrink original bitmap to new size
      BitmapNew.Canvas.StretchDraw( BitmapNew.Canvas.ClipRect,
BitmapOriginal);

    except
      BitmapNew.Free;
    end;

    result := BitmapNew;
end;

procedure TfmImage.Resize1Click(Sender: TObject);
begin
  // Display new, smaller bitmap
  Image1.Picture.Graphic := ResizeBMP( Image1.Picture.Bitmap , 100 , 100 );
  Image1.Refresh;
end;

 

Re:Keep getting Blank (White) image instead of resized bitmap


Well its like this.
  you are freeing the very bitmap that your trying to  define for the return.
also take note that reading the bitmap object from timage for the first time use
to
delete the original and recreate a new one thus giving you a simple blank image
to start with..
 so take that into consideration..
 you may want to create a Bitmap and set the pixel format to 24bit on start up
and directly set it to the
Timage.picture.bitmap in the FormCreate method..
Quote
arjang wrote:
> I tried number of variations of bitmap resizing I found from efg and this
> newsgroup.
> I keep getting the same problem, the resized image comes up white, any ideas
> of what I
> am doing wrong?
> TIA

> function ResizeBMP(const BitmapOriginal:TBitMap;
> NuWidth,NuHeight:integer):TBitMap;
> var
>   BItmapNew     :  TBitmap;
> begin
>     BitmapNew := TBitmap.Create;
>     try
>       BitmapNew.Width  := NuWidth;
>       BitmapNew.Height := NuHeight;
>       BitmapNew.PixelFormat := pf24bit;

>       // Shrink original bitmap to new size
>       BitmapNew.Canvas.StretchDraw( BitmapNew.Canvas.ClipRect,
> BitmapOriginal);

>     except
>       BitmapNew.Free;
>     end;

>     result := BitmapNew;
> end;

> procedure TfmImage.Resize1Click(Sender: TObject);
> begin
>   // Display new, smaller bitmap
>   Image1.Picture.Graphic := ResizeBMP( Image1.Picture.Bitmap , 100 , 100 );
>   Image1.Refresh;
> end;

Re:Keep getting Blank (White) image instead of resized bitmap


You have to convert the image to a bitmap *before* loading it into the Image
component.  That's the only way that the bitmap property will have a valid
value.

So whereever your load command is do something like (not tested):
var B : TBitmap;
begin
   Image1.picture.loadfromfile(myfile);
   B := TBitmap.Create;
   try
      B.Assign(Image1.picture.graphic);
      Image1.Picture.Bitmap.Assign(B);
   finally
      B.Free;
   end;
end;

Quote
"arjang" <nos...@here.com> wrote in message

news:3def794b@newsgroups.borland.com...
Quote
> I tried number of variations of bitmap resizing I found from efg and this
> newsgroup.
> I keep getting the same problem, the resized image comes up white, any
ideas
> of what I
> am doing wrong?
> TIA

> function ResizeBMP(const BitmapOriginal:TBitMap;
> NuWidth,NuHeight:integer):TBitMap;
> var
>   BItmapNew     :  TBitmap;
> begin
>     BitmapNew := TBitmap.Create;
>     try
>       BitmapNew.Width  := NuWidth;
>       BitmapNew.Height := NuHeight;
>       BitmapNew.PixelFormat := pf24bit;

>       // Shrink original bitmap to new size
>       BitmapNew.Canvas.StretchDraw( BitmapNew.Canvas.ClipRect,
> BitmapOriginal);

>     except
>       BitmapNew.Free;
>     end;

>     result := BitmapNew;
> end;

> procedure TfmImage.Resize1Click(Sender: TObject);
> begin
>   // Display new, smaller bitmap
>   Image1.Picture.Graphic := ResizeBMP( Image1.Picture.Bitmap , 100 ,
100 );
>   Image1.Refresh;
> end;

Re:Keep getting Blank (White) image instead of resized bitmap


probably you are using a too big stretch ratio : stretchdraw has some limit
in the stretch ratio (maybe between 8:1 and 10:1).
Try  to search this newsgroup on google: you should find resize and resample
routines that don't use stretchdraw.

"arjang" <nos...@here.com> ha scritto nel messaggio
news:3def794b@newsgroups.borland.com...

Quote
> I tried number of variations of bitmap resizing I found from efg and this
> newsgroup.
> I keep getting the same problem, the resized image comes up white, any
ideas
> of what I
> am doing wrong?
> TIA

> function ResizeBMP(const BitmapOriginal:TBitMap;
> NuWidth,NuHeight:integer):TBitMap;
> var
>   BItmapNew     :  TBitmap;
> begin
>     BitmapNew := TBitmap.Create;
>     try
>       BitmapNew.Width  := NuWidth;
>       BitmapNew.Height := NuHeight;
>       BitmapNew.PixelFormat := pf24bit;

>       // Shrink original bitmap to new size
>       BitmapNew.Canvas.StretchDraw( BitmapNew.Canvas.ClipRect,
> BitmapOriginal);

>     except
>       BitmapNew.Free;
>     end;

>     result := BitmapNew;
> end;

> procedure TfmImage.Resize1Click(Sender: TObject);
> begin
>   // Display new, smaller bitmap
>   Image1.Picture.Graphic := ResizeBMP( Image1.Picture.Bitmap , 100 ,
100 );
>   Image1.Refresh;
> end;

Re:Keep getting Blank (White) image instead of resized bitmap


Hi

Quote
>       BitmapNew.Width  := NuWidth;
>       BitmapNew.Height := NuHeight;
>       BitmapNew.PixelFormat := pf24bit;

    You are changing pixel depth after you have specified size of new image.
Specifying pixel-depth makes the size of the image 0x0. Convert above to:

       BitmapNew.PixelFormat := pf24bit;
       BitmapNew.Width  := NuWidth;
       BitmapNew.Height := NuHeight;

Tomaz

Quote
"arjang" <nos...@here.com> wrote in message

news:3def794b@newsgroups.borland.com...
Quote
> I tried number of variations of bitmap resizing I found from efg and this
> newsgroup.
> I keep getting the same problem, the resized image comes up white, any
ideas
> of what I
> am doing wrong?
> TIA

> function ResizeBMP(const BitmapOriginal:TBitMap;
> NuWidth,NuHeight:integer):TBitMap;
> var
>   BItmapNew     :  TBitmap;
> begin
>     BitmapNew := TBitmap.Create;
>     try
>       BitmapNew.Width  := NuWidth;
>       BitmapNew.Height := NuHeight;
>       BitmapNew.PixelFormat := pf24bit;

>       // Shrink original bitmap to new size
>       BitmapNew.Canvas.StretchDraw( BitmapNew.Canvas.ClipRect,
> BitmapOriginal);

>     except
>       BitmapNew.Free;
>     end;

>     result := BitmapNew;
> end;

> procedure TfmImage.Resize1Click(Sender: TObject);
> begin
>   // Display new, smaller bitmap
>   Image1.Picture.Graphic := ResizeBMP( Image1.Picture.Bitmap , 100 ,
100 );
>   Image1.Refresh;
> end;

Re:Keep getting Blank (White) image instead of resized bitmap


Quote
In article <3df0a...@newsgroups.borland.com>, Tomaz Koritnik wrote:
> You are changing pixel depth after you have specified size of new image.
> Specifying pixel-depth makes the size of the image 0x0. Convert above to:

It's a better way to do it IMO but it shouldn't stop it working. It just
means the VCL has to make a copy of a big bitmap (unnecessarily) instead of
a small one. I can't see why it shouldn't work other than the problem
Francesco pointed out of over stretching.

Gordon

Re:Keep getting Blank (White) image instead of resized bitmap


Quote
In article <3def7...@newsgroups.borland.com>, Arjang wrote:
> I keep getting the same problem, the resized image comes up white, any ideas
> of what I
> am doing wrong?

That's probably because the first picture that you loaded into image1 was not
a bitmap to begin with. See comments in code below. If that wasn't the
problem, then Francesco's comment might apply. It's not safe to stretch
something by a factor of more than 8.

But also, your code creates a memory leak. I would never code a routine which
creates an object without at least giving it a name clearly stating this fact.
Something like:

CreateResizedBitmap(....)

Try whether the following replacement fixes your problem.

Renate

procedure TfrmImage.Resize1Click(Sender: TObject);
var bm: TBitmap;
begin
  // Display new, smaller bitmap

  if not (Image1.Picture.Graphic is TBitmap) then
  //if this is the case then any mentioning of
  //image1.picture.bitmap erases the current content
  begin
    bm:=TBitmap.Create;
    try
      bm.pixelformat:=pf24bit;
      //setting pixelformat after setting
      //the width and height slows things down
      //but it does not set width/height to 0.
      bm.width:=image1.Picture.Width;
      bm.height:=image1.Picture.Height;
      bm.canvas.Draw(0,0,image1.Picture.graphic);
      image1.Picture.Graphic:=bm;
    finally
      bm.free;
    end;
  end;
  bm:=CreateResizedBMP( Image1.Picture.Bitmap , 100 , 100 );
  //without having a reference to the result,
  //the bitmap can't be freed
  try
    Image1.Picture.Graphic:=bm;
  finally
    bm.free;
  end;
end;

Re:Keep getting Blank (White) image instead of resized bitmap


Quote
In article <VA.0000026e.01f46...@xmission.com>, Renate Schaaf wrote:
> But also, your code creates a memory leak. I would never code a routine which
> creates an object without at least giving it a name

It's common enough in the VCL although it's always:

  except
    Something.free;
    raise
    end;

Gordon

Re:Keep getting Blank (White) image instead of resized bitmap


Quote
>     You are changing pixel depth after you have specified size of new
image.
> Specifying pixel-depth makes the size of the image 0x0.

This isn't true: you can assign the pixelformat wherever you want in the
code (except before the create method) and this doesn't affect bitmap's
width and height. It is true though that there are other good reasons to
assign the pixelformat just after the create method call.

Re:Keep getting Blank (White) image instead of resized bitmap


Quote
In article <VA.000000ae.013cc...@mgcsoft.com>, Gordon Whittam wrote:
> It's common enough in the VCL although it's always:

>   except
>     Something.free;
>     raise
>     end;

But only within the implementation of a VCL-class, and that's Ok. Look,
I didn't say you *shouldn't* write methods which return an object
created by the method, only that you should indicate the fact by giving
it a telling name, at least while one's not a member of the Delphi RAD
yet :). I'd personally never code a utility routine that way though,
because it's asking for being bitten later. The way arjang got trapped
is a perfect example.

Renate

Re:Keep getting Blank (White) image instead of resized bitmap


Quote
In article <3df21...@newsgroups.borland.com>, Arjang wrote:
> The code was meant to be short to point out the problem with
> getting back a blank result only.

Have you solved it?

Gordon

Other Threads