Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Scaling graphics


This question is answered.


Permlink Replies: 8 - Last Post: Sep 10, 2014 4:16 AM Last Post By: John Smith
John Smith

Posts: 13
Registered: 6/12/09
Scaling graphics  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 8, 2014 8:28 AM
I have a number of bitmaps sized at 96x71 pixels. They show at their native size on my form which is fixed at 800x600 but can be maximised to full screen (but not anything in between). The images are drawn in a TImage using StretchDraw but for large screens look terrible. They also look terrible if I use a larger bitmap and scale down. I would like to use vector graphics to improve the image quality when scaled and have all the images in EPS format, but I can't find any information on how to load an EPS image using Delphi.

Anybody point me to information on how to get an EPS image drawn on a vcl form either via a TImage or some other control?

Using XE3 and Windows 32 bit is the target.
Slobodan Dmitro...

Posts: 75
Registered: 8/20/11
Re: Scaling graphics  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 8, 2014 10:14 AM   in response to: John Smith in response to: John Smith
Is using a Direct2D an option? If so, TDirect2DCanvas scales images quite nicely. Sample code:
var
  MyImage: TWICImage;
  MyCanvas: TDirect2DCanvas;
begin
  // Load image
  if TDirect2DCanvas.Supported then
  begin
    if OpenDialog1.Execute then
    begin
      MyImage := TWICImage.Create;
      MyCanvas := TDirect2DCanvas.Create(Canvas, ClientRect);
      try
        MyCanvas.BeginDraw;
        MyCanvas.RenderTarget.Clear(D2D1ColorF(clBtnFace));
        MyImage.LoadFromFile(OpenDialog1.FileName);
        // Draw original image:
        // MyCanvas.Draw(0, 0, MyImage, 255);
        // Draw scaled image:
        MyCanvas.StretchDraw(Rect(0, 0, MyImage.Width * 2, MyImage.Height * 2),
          MyImage, 255);
      finally
        MyCanvas.EndDraw;
        MyCanvas.Free;
        MyImage.Free;
      end;
    end;
  end
  else
    ShowMessage
      ('Direct2D not supported.');
end;

Include Direct2D and D2D1 units. This is just a sample code to be adapted and moved to OnPaint event.
John Smith

Posts: 13
Registered: 6/12/09
Re: Scaling graphics  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 8, 2014 2:52 PM   in response to: Slobodan Dmitro... in response to: Slobodan Dmitro...
Slobodan Dmitrovic wrote:
Is using a Direct2D an option? If so, TDirect2DCanvas scales images quite nicely. Sample code:

Thanks - maybe a partial solution but I have many users on XP which does not support Direct2D as far as I know.

Edited by: John Smith on Sep 9, 2014 1:20 AM
Slobodan Dmitro...

Posts: 75
Registered: 8/20/11
Re: Scaling graphics
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 9, 2014 7:52 AM   in response to: John Smith in response to: John Smith
Thanks - maybe a partial solution but I have many users on XP which does not support Direct2D as far as I know.
You are most welcome. Take a look at this SO answer. User TLama has provided an extensive demo in his answer:
http://stackoverflow.com/a/13595922/1593881
John Smith

Posts: 13
Registered: 6/12/09
Re: Scaling graphics  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 9, 2014 8:38 AM   in response to: Slobodan Dmitro... in response to: Slobodan Dmitro...
Slobodan Dmitrovic wrote:
Take a look at this SO answer. User TLama has provided an extensive demo in his answer:
http://stackoverflow.com/a/13595922/1593881
The code on that page seems to do a great job - even lets me use jpg as the source with no obvious loss of clarity which saves a lot on file size (I have 52 images!) .
Douglas Rudd

Posts: 314
Registered: 5/16/97
Re: Scaling graphics  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 8, 2014 10:38 AM   in response to: John Smith in response to: John Smith
Is the original a vector image or a bitmap image? If it is a bitmap image then you are not going to turn it into a vector image.
And I don't see any Delphi components out there for displaying EPS pictures.
Or if you already have vector EPS pictures, if you could translate them to WMF files, those would be easy to display in Delphi.

I would go the route of having a large bitmap and shrinking it when needed.
Stretchdraw might not make a quality shrink image, but there are techniques and components out there for making a better quality one than your normal Stretchdraw.
Look up "delphi create thumbnail image".
John Smith

Posts: 13
Registered: 6/12/09
Re: Scaling graphics  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 8, 2014 2:50 PM   in response to: Douglas Rudd in response to: Douglas Rudd
Douglas Rudd wrote:
Is the original a vector image or a bitmap image? If it is a bitmap image then you are not going to turn it into a vector image.
And I don't see any Delphi components out there for displaying EPS pictures.
Or if you already have vector EPS pictures, if you could translate them to WMF files, those would be easy to display in Delphi.

I would go the route of having a large bitmap and shrinking it when needed.
Stretchdraw might not make a quality shrink image, but there are techniques and components out there for making a better quality one than your normal Stretchdraw.
Look up "delphi create thumbnail image".
Thanks for this. I gave it a try creating a wmf from the EPS file with PaintShopPro. In PSP the wmf file can be zoomed in or out and the image looks quite good but the code below gives mixed results. Image1 is a little less than half the size of the wmf and Image 2 a bit more than twice the size. Image2 displays very well but Image1 is useless. Result is the same with stretchdraw and also if I use PSP to reduce the wmf image size (ie PSP displays the image nicely when zoomed to 50% but it looks horrible if I save a file with the image size reduced to 50%). Are there better ways to reduce the image ?

procedure TForm1.BitBtn1Click(Sender: TObject);
var
MetaFile: TMetafile;
begin
Metafile := TMetaFile.Create;
try
MetaFile.LoadFromFile(MywmfFile);
MetaFile.SetSize(Image1.Width, Image1.Height);
Image1.Canvas.Draw(0, 0, MetaFile);
MetaFile.SetSize(Image2.Width, Image2.Height);
Image2.Canvas.Draw(0, 0, MetaFile);
finally
MetaFile.Free;
end;
end;
Douglas Rudd

Posts: 314
Registered: 5/16/97
Re: Scaling graphics
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 9, 2014 12:49 PM   in response to: John Smith in response to: John Smith
You are running into the same problem. The small image is not anti-aliased/compressed so it looks choppy.
I tried this code to take the large image and shrink it to the small image.
http://www.swissdelphicenter.com/torry/showcode.php?id=1945

Try it and you can see it makes a better looking small image than normal.
John Smith

Posts: 13
Registered: 6/12/09
Re: Scaling graphics  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 10, 2014 4:16 AM   in response to: Douglas Rudd in response to: Douglas Rudd
Douglas Rudd wrote:
You are running into the same problem. The small image is not anti-aliased/compressed so it looks choppy.
I tried this code to take the large image and shrink it to the small image.
http://www.swissdelphicenter.com/torry/showcode.php?id=1945

Try it and you can see it makes a better looking small image than normal.
This works reasonably well and has the advantage of being much simpler than the alternative above, and I can understand the code, and it also works for older Windows versions, so many thanks.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02