Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a SaveToFile question 2

Status
Not open for further replies.

d2ned

Programmer
Jun 27, 2003
37
FI
Hey delphi people,

I'm struggling with newbie questions again, I'm supposed to conjure up an app that's supposed to

A) let the user browse for a image file on his/her computer

B) show a TImage of what he chose

C) automatically thumbnail that image to a 100*75 px

D) then save both files to a server folder

now the A and B even a delphi newbie like me could handle but C and D are proving to be a quite a handful.

For some time now I've been trying to get my app to FTP them over to the server but I've had NO luck at all. So I thought would it be easier to somehow just save those files (the original pic & thumbnail) to the server location, because both the users computer and the server are on the same network, and I could give full rights to the users computer to the server? Or am I wrong? Is there a easy source for FTP:ing a file to a specified location?

What I've simply done so far, if this clears this up at all, is
Code:
procedure TMyApp.AddPicture1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
   begin
   if OpenPictureDialog1.FileName <> '.' then
   filename1 := OpenPictureDialog1.FileName;
   Edit1.Text := filename1;
   Image1.Picture.LoadFromFile(OpenPictureDialog1.Filename);
   end;
end;

I'd really need ANY help you guys/gals could spare on this, this app has been bugging me for a long time.. :/

-J
 
try this to scale

Code:
procedure copybmp(ASrc,ADst: TBitmap);
var
	r,c: Integer;
	x,y: Integer;
	dx,dy: Real;
begin
(*
it assumes dst is always smaller then src, 
and that both bitmaps have their dimensions set
*)
	dx := ASrc.Width  / ADst.Width;
	dy := ASrc.Height / ADst.height;

	for r := 0 to ADst.Height-1 do
	begin
		y := Floor(r * dy);
		for c := 0 to ADst.Width-1 do
		begin
			x := Round(c * dx);
			ADst.Canvas.Pixels[c,r] := ASrc.Canvas.Pixels[x,y];
		end;
	end;
end;

procedure TForm1.BitBtnConvert(Sender: TObject);
begin
	ImageSrc.LoadFromFile(OpenDialog1.FileName);
	ImageDst.Picture.Bitmap.Width  := 75;
	ImageDst.Picture.Bitmap.Height := 100;
	copybmp(ImageSrc.Picture.Bitmap,ImageDst.Picture.Bitmap);
	ImageSrc.Invalidate;
end;

to save just go:

Code:
Image1.Picture.SaveToFile('thenewfilename.bmp');

hth

- fruNNik

 
Hi,

and thank You very much fruNNik, I'll try ur code tonight. I'll let u know how it works out.

-J
 
Hi fruNNik,

I figured the part

y := Floor(r * dy);

probably meant the math function floor, right? So I pushed my newbie skills and added Math to my USES clause :), but I'm getting an error message about ur syntax on that particular calculation above,

[Error] MP.PAS(127): Missing operator or semicolon

to be precise, and far as I can see, ur syntax is just fine. I'm on D7, anyone have any idea why this is? But so far looking pretty DAMN good.. just to get this to work..
Here's a star for Your troubles so far!

-J
 
Well you can cancel that last post, I just rebooted and tried again - lo and behold; it's working like a charm! It probably was just some weird memory bug but hey, who cares, it's working now! Thank You a million times fruNNik, I owe you a beer!

-J
 
Just one last question.. almost all the images that this app is going to process are jpegs, and for some reason this code won't accept jpeg files, though i thought TBitmap could? (If I choose an jpeg file, it leaves ImageSrc empty and shows an black ImageDst, probably because it's copying from, well, nothing.)

Is this part the reason?

copybmp(ImageSrc.Picture.Bitmap,ImageDst.Picture.Bitmap);

Because .bmp files are working just great! The answer is very probably something very, very simple that I'm just missing.

-J
 
Great it works!

To use jpeg u need to include the unit jpeg...
You probably need to convert the copybmp function to accept a TGraphic instead of a TBitmap ... dunno ?

After i gave you my solution, the following occured to me:

You could probably just use the Canvas.CopyRect function to
do in one call what i did with copybmp.

I didnt check that last part though...

If u need more help lemme know...

Cheers

- fruNNik
 
Hi,

yes my app already has jpeg included, and I'm still having troubles copying that jpegimage..

The fact that it opens jpeg images ok, as long as I don't do anything involving the Bitmap property proves that that's the problem... and the delphi help had this in it:
The following are characteristics of this object. A TJPEGImage object:
[ul]
[li]Has no canvas (so it cannot draw onto a canvas). However, TJPEGImage implements the protected Draw method introduced in TGraphic, so it can draw itself on the canvas of another object.[/li]
[li]Provides no access to the internal bitmap image that it creates for the JPEG image.[/li]
[li]Performs reference counting and handle sharing by means of the TJPEGData object. Multiple instances can refer to the same TJPEGData image. TJPEGData is the actual owner of the file handle to the jpeg data source.[/li]
[/ul]
So it would seem that a jpeg image has no canvas at all. So, basically, it can't be edited, right? Which then again means that I would have to first copy that image on to a canvas that CAN be edited, is that it?

Unfortunately, I have no idea how this can be done. Would I have to create a third image that contains the "fake" or "in-between" canvas? Any ideas, anyone?

-J
 
If you can get your JPG file in a TImage, use this code to scale it:

Code:
function .ScaleImage(Img           : TImage; 
                     Width, Height : cardinal
                     ) : TBitmap;
  var
    R : TRect;
  begin
    Result := TBitmap.Create;
    Result.Width := Width;
    Result.Height := Height;
    FillChar(R, SizeOf(R), 0);
    R.Right := Width;
    R.Bottom := Height;
    Result.Canvas.StretchDraw(R, Img.Picture.Graphic);
  end;

var
  BMP : TBitmap;
begin
  // How to use
  Image1.Picture.LoadFromFile('xxxx.jpg');
  BMP := ScaleImage(Image1, 100, 75);
  BMP.SaveToFile('scaled.bmp');
  BMP.Free;
end;

Actually, the code will work with any image format TImage can load.

buho (A).
 
Thx guys, I got my app in a working order now! All I got to do now is to get my app to save the thumbnail as an jpg as well, but that part is easy!

There seems to be a lot of misconceptions floating around about the use of TImage, TBitmap & TJPEG, I have to consider writing a faq about this in here for the rest of us delphi newbies.

But thanks again both of u for Your invaluable help!

-J
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top