Link to home
Start Free TrialLog in
Avatar of christina sen
christina sen

asked on

delphi

how to save image in image object

if SavePictureDialog1.Execute then
Image1.Picture.SaveToFile(SavePictureDialog1.FileName);
this is not happening
Avatar of Ganesh Gurudu
Ganesh Gurudu

Try like this

if SavePictureDialog1.Execute then
    { first check if file exists }
    if FileExists(SavePictureDialog1.FileName) then
      { if it exists, raise an exception }
      raise Exception.Create('File already exists. Cannot overwrite.')
    else
      { otherwise, save the image data into the file }
      Image1.Picture.SaveToFile(SavePictureDialog1.FileName);
Avatar of Sinisa Vuk
What type is you image in Image1? (bmp, jpg, ...)
try to use: Image1.Picture.Graphic.SaveToFile(...)
Avatar of christina sen

ASKER

Ganesh Gurudu I tried to write code but I do save the file in the form of file, I want the jpeg extension
Do you have jpeg stored in TImage object?  Do you load it in design time?
why dont you trying converting the extension and save with that extension only

var
  s : string;
  Icon: TIcon;
begin
  OpenDialog1.DefaultExt := '.ICO';
  OpenDialog1.Filter := 'icons (*.ico)|*.ICO';
  OpenDialog1.Options := [ofOverwritePrompt, ofFileMustExist, ofHideReadOnly ];
  if OpenDialog1.Execute then
  begin
    Icon := TIcon.Create;
    try
      Icon.Loadfromfile(OpenDialog1.FileName);
      s:= ChangeFileExt(OpenDialog1.FileName,'.BMP');
      Image1.Width := Icon.Width;
      Image1.Height := Icon.Height;
      Image1.Canvas.Draw(0,0,Icon);
      Image1.Picture.SaveToFile(s);
      ShowMessage(OpenDialog1.FileName + ' Saved to ' + s);
    finally
      Icon.Free;
    end;
  end;
end;
I say the first time I wrote the code works, but when I write the drawing.jpeg I am having trouble opening the file at this time
ı would say so. the code you wrote first works , but when ı save it, the drawing.jpeg is saved. ı am having a hard time filing this file
Please, explain more what you trying to do? Which image you need to save where?
I only get that you need to save in jpeg file format.

Ganesh ... you cannot load bmp file into TIcon object.
sorry i can not tell;
I am making a drawing program I am saving a folder in the image that is drawn in the image with the following code;

SavePictureDialog1.Execute
Image2.Picture.SaveToFile(SavePictureDialog1.FileName);

but when saving with this code, it saves it as a file, so I found the solution as follows: I save the drawing as a drawing window.jpg when saving, I can save it as a picture afterwards;
this time when I click on the image file open button that I saved as jpeg, it gives an error :(
This is because it is not jpeg file format but .bmp I assume. If you choose in SavePictureDialog1 to save as jpeg - then  you need to conver it before save...

if SavePictureDialog1.Execute then
begin
  if UpperCase(ExtractFileExt(SavePictureDialog1.FileName)) = '.JPG' then
  begin
    //save as jpeg file...
    if Image2.Picture.Graphic is TJpegImage then
      Image2.Picture.SaveToFile(SavePictureDialog1.FileName); //save as is
    else  
      Bmp2Jpeg(...); //convert bmp to jpeg and save to jpeg file
  end;
  if UpperCase(ExtractFileExt(SavePictureDialog1.FileName)) = '.BMP' then
  begin
    //save as bmp
     if Image2.Picture.Graphic is TBitmap then
      Image2.Picture.SaveToFile(SavePictureDialog1.FileName); //save as is
    else  
      Jpeg2Bmp(...); //convert jpg to bmp and save to bmp file
  end;
  ....
end;

Open in new window


There are example how to convert bmp to jpeg ...
can you provide the error message with error code
Genesh,
i doubt there is an error message
it's as sinisa says.
it's an image saved with a wrong extension

changing a file extension doesn't change the contents

if you rename iexplore.exe to iexplore.bmp and then open iexplore.bmp,
i'm certain you won't see a bitmap image on the screen
christina
if you are drawing inside your form with the mouse, then you are drawing on a bitmap
not on a jpeg.

if you want to draw on a jpeg, you need to convert it to a bitmap first upon loading it
then you can draw on the bitmap
upon save, you need to convert to jpeg again
yapmak istediğim sadece image üzerine çizim yapmak ve bunu resim uzantılı kaydetmek
uhm yeah... english ?

there is no such easy thing as just drawing.
only if it's on a bitmap

so you need to convert the source (when it's not bitmap into bitmap)
and for saving convert it back to the source format

here's a tutorial on creating your own paint program:
http://delphi.wikia.com/wiki/Create_Your_Own_Paint_Program
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.