Solved

SaveDialog1 Question ...

Posted on 2000-05-12
26
666 Views
Last Modified: 2010-04-04
I'm trying to create a save dialog that will give the option to save the contents of an image box to a bitmap, jpeg, jpg, or wmf.  I have the code to save in all the formats, but currently i'm using a different menu item for each save that sets the filter and defualt extention before the save.

example: (bitmap)

// Save as Bitmap menu item or toolbutton selected
// ===============================================
procedure TForm1.AsBitmap1Click(Sender: TObject);
var
  bmp: TBitmap;

BEGIN
  // set file extention
  SaveDialog1.DefaultExt := 'bmp';
  SaveDialog1.Filter := 'Bitmap Image (*.bmp)|*.bmp';
  If savedialog1.Execute then
  BEGIN
    bmp := TBitmap.create;
    TRY
      bmp.Assign(Image1.Picture.Bitmap);
      bmp.SaveToFile(savedialog1.filename);
      statusbar1.Panels[1].Text := savedialog1.filename;
    FINALLY
      bmp.Free
    END;
  END;

end;
// ================================================


I would rather the user just select save and the dialog box pops up with the filter options, then the save is conducted according to what file type they selected.  I'm having trouble with the conditional part of this, but here is what I've got so far (it's not working).

procedure TForm1.SaveAs1Click(Sender: TObject);
var
  // save variables
  DoBitmap: Boolean;
  DoJpg:    Boolean;
  DoJpeg:   Boolean;
  DoWmf:    Boolean;
  // image variables
  bmp: TBitmap;
  JPEG: TJPEGImage;
begin
  If Savedialog1.Execute then
    If Savedialog1.Filter = 'Bitmap' then DoBitmap := true
    else
    If Savedialog1.filter = '*.jpg' then DoJpg := true
    else
    If SaveDialog1.filter = '*.jpeg' then DoJpeg := true
    else
    If SaveDialog1.filter = '*.wmf' then DoWmf := true;

    If DoBitmap = true then
    begin
      // set file extention
     SaveDialog1.DefaultExt := 'bmp';
     SaveDialog1.Filter := 'Bitmap Image (*.bmp)|*.bmp';
     If savedialog1.Execute then
     BEGIN
     bmp := TBitmap.create;
     TRY
      bmp.Assign(Image1.Picture.Bitmap);
      bmp.SaveToFile(savedialog1.filename);
      statusbar1.Panels[1].Text := savedialog1.filename;
     FINALLY
      bmp.Free
    END;
  END;
  end;
  // other file types left out until I get this working

end;
0
Comment
Question by:fibdev
  • 12
  • 10
  • 4
26 Comments
 
LVL 3

Author Comment

by:fibdev
Comment Utility
Also,

I already tried this:

If Savedialog1.Filter = 'Bitmap' then DoBitmap := true

Like this:

If Savedialog1.Filter = '*.bmp' then DoBitmap := true
0
 
LVL 15

Expert Comment

by:simonet
Comment Utility
SImply use the FilterIndex property of the TSaveDialog component.

The first filter in the list has an index of 1 (ONE). After the user clicks on "Save", the FilterIndex property is set with the filter the user chose.

Example:

type
  TPicFormat = (pfNone, pfBitmap, pfMetafile, pfJPEG, pfPhotoshop);

var
  PicFormat : TPicFormat;
begin
//  (snip)
  with SaveDialog1 do
  begin
     Filter := 'Bitmap|*.bmp|Metafile|*.wmf|JPEG|*.JPG|Photoshop|*.psd';
     if Execute then
        PicFormat  := TPicFormat(FilterIndex)
     else
        PicFormat := pfNone;
  end;
  case PicFormat of
      pfNone : exit;
      pfBitmap : DoBitmap;
      pfMEtafile : DoMetafile;
      pfJPEG : DoJPEG;
      pfPhotoshop : DoPhotoshop;
  end;
end;

Something very similar to the code above will work just fine for you.

Yours,

Alex
 
0
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
use FilterIndex:

  DoBmp:= False;
  DoJpg:= False;
.. ...
if SaveDialog1.Execute then
begin
  case SaveDialog1.FilterIndex of
    1: DoBmp:= True;
    2: DoJpg:= True;
    3: DoJpeg:= True;
    4: DoWmf:= True;
  end; // case
  ...
end;

Motaz
www.geocities.com/motaz1
0
 
LVL 15

Accepted Solution

by:
simonet earned 75 total points
Comment Utility
I meant that as an answer, fibdev, but EE wouldn't let me post that as an answer. It would give me some weird message about key violation of their Oracle databases. Go figure.

Anyway, the code above will work just fine for you. If you had troubles with it, just let me know, ok?

Yours,

Alex
0
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
Alex, I also encounter this problem while attempting to answer this question, but you are more lucky than me:-)

Motaz
0
 
LVL 15

Expert Comment

by:simonet
Comment Utility
Blame your modem, Motaz! 1 minute is pretty close, isn't it?
0
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
By the way Alex two days ago we read your CV that I downloaded two years ago from your site, we select this document as to test our project (Digital library) cgi. I think you started computer the same age like me and with the same tools, MSX, tape drive... but now you are one of the best Delphi programmers in the world.
How can I be like you? what did you do to reach this point?

Motaz
www.geocities.com/motaz1
0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
Please forgive me as I am new to Delphi, but how do I modify this to execute my save rutines:

  case PicFormat of
      pfNone : exit;
      pfBitmap : DoBitmap;
      pfMEtafile : DoMetafile;
      pfJPEG : DoJPEG;
      pfPhotoshop : DoPhotoshop;
  end;

Alex,

Could you please change your example to actually execute a save as bitmap, I can figure out the rest of them.

Thank you,
Gabe
0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
Ok,

Here is what I got, but it doesn't include the extention, just the name of the file:

procedure TForm1.SaveAs1Click(Sender: TObject);
type
  TPicFormat = (pfNone, pfBitmap, pfMetafile, pfJPEG, pfPhotoshop);
var
  bmp: TBitmap;
  PicFormat : TPicFormat;
  DoBitmap : Boolean;
  DoMetafile : Boolean;
  DoJPEG : Boolean;
begin
//  (snip)
  with SaveDialog1 do
  begin
     Filter := 'Bitmap|*.bmp|Metafile|*.wmf|JPEG|*.JPG';

     if Execute then
        PicFormat  := TPicFormat(FilterIndex)
     else
        PicFormat := pfNone;
  end;
  case PicFormat of
      pfNone : exit;
      pfBitmap : DoBitmap := true;
      pfMEtafile : DoMetafile := true;
      pfJPEG : DoJPEG := true;
  end;

  If DoBitmap = True then begin
  bmp := TBitmap.create;
    TRY
      bmp.Assign(Image1.Picture.Bitmap);
      bmp.SaveToFile(savedialog1.filename);
      statusbar1.Panels[1].Text := savedialog1.filename;
    FINALLY
      bmp.Free
    END;
    end;

    // Windows Metafile
   If DoMetafile = True then begin
    SaveDialog1.DefaultExt := 'wmf';
    bmp := TBitmap.create;

    TRY
      bmp.Assign(Image1.Picture.Bitmap);
      bmp.SaveToFile(savedialog1.filename);
      statusbar1.Panels[1].Text := savedialog1.filename;
    FINALLY
      bmp.Free
    END;
  END;

 end;

If I could resolve the issue of no extention, I think I will work fine.
0
 
LVL 15

Expert Comment

by:simonet
Comment Utility
What's the problem of "no extension"?

Is it to prevent the user from selecting a different extension than those listed in the Filters property?

You can add some code for that:

var
 InvalidExt, Canceled : boolean;
 Ext : string;

//<snip>
begin

  with SaveDialog1 do
  begin
     Filter := 'Bitmap|*.bmp|Metafile|*.wmf|JPEG|*.JPG';
     // Check for invalid file extensions
     InvalidExt := false;
     Canceled := true;
     while ((not canceled) or InvalidExt) do
     begin
        Canceled := Execute;
        If (not Canceled) then
        begin
           PicFormat  := TPicFormat(FilterIndex);
           Ext := uppercase(ExtractFileExt(FileName));
           InvalidExt := (Ext<>'.BMP') or (Ext<>'.EMF') or (Ext<>'.WMF') or (Ext<>'.JPG') or (Ext<>'.JPEG');
        end
        else
              PicFormat := pfNone;
     end;
  end;
  // The rest of the code statys the same

Yours,

Alex
0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
Sorry to be unclear,

Using the code I posted ...

Unless you type the filename "file.ext" it just save the file as "file" with no extention.  How do I change the code I posted to include the extention.
0
 
LVL 15

Expert Comment

by:simonet
Comment Utility
Ok, this is easy to solve:

Replace the entire "if Execute then" clause for this:



if Execute then
begin
   if (ExtractFileExt(FileName)='') then
      case FilterIndex of
         1 : FileName := FileName + '.BMP';
         2 : FileName := FileName + '.WMF';
         3 : FileName := FileName + '.JPG';
         default : FileName := FileName + '.BMP';
      end;
      PicFormat  := TPicFormat(FilterIndex);
   end
else
   PicFormat := pfNone;


     
Remember to declare the Ext variable:

var
  Ext : string;


Yours,

Alex
 
0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
This code:

procedure TForm1.SaveAs1Click(Sender: TObject);
type
  TPicFormat = (pfNone, pfBitmap, pfMetafile, pfJPEG, pfPhotoshop);
var
  bmp: TBitmap;
  PicFormat : TPicFormat;
  DoBitmap : Boolean;
  DoMetafile : Boolean;
  DoJPEG : Boolean;
  Ext : string;

begin
//  (snip)
  with SaveDialog1 do
  begin
     Filter := 'Bitmap|*.bmp|Metafile|*.wmf|JPEG|*.JPG';

if Execute then
begin
   if (ExtractFileExt(FileName)='') then
      case FilterIndex of
         1 : FileName := FileName + '.BMP';
         2 : FileName := FileName + '.WMF';
         3 : FileName := FileName + '.JPG';
         default : FileName := FileName + '.BMP';
      end;
      PicFormat  := TPicFormat(FilterIndex);
   end
else
   PicFormat := pfNone;


  If DoBitmap = True then begin
  bmp := TBitmap.create;
    TRY
      bmp.Assign(Image1.Picture.Bitmap);
      bmp.SaveToFile(savedialog1.filename);
      statusbar1.Panels[1].Text := savedialog1.filename;
    FINALLY
      bmp.Free
    END;
    end;

    // Windows Metafile
   If DoMetafile = True then begin
    SaveDialog1.DefaultExt := 'wmf';
    bmp := TBitmap.create;

    TRY
      bmp.Assign(Image1.Picture.Bitmap);
      bmp.SaveToFile(savedialog1.filename);
      statusbar1.Panels[1].Text := savedialog1.filename;
    FINALLY
      bmp.Free
    END;
  END;

 end;




produced this:

[Error] Unit1.pas(540): Undeclared identifier: 'default'
[Error] Unit1.pas(577): ';' expected but '.' found
[Error] Unit1.pas(581): Declaration expected but end of file found
[Fatal Error] pScrnShot.dpr(11): Could not compile used unit 'Catch\Unit1.pas'
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 15

Expert Comment

by:simonet
Comment Utility
To correct all the errors:

1) Remove the following line:

  default : FileName := FileName + '.BMP';
     
in the case statement (line 540). This line is to be deleted.

2) The very last "end;" (at line 577, I believe), should be replaced with:

end.  // <-- note that this is a period, not a semi-colon


This will make it compile fine and run ok (if there are no errors in other parts of your project).

Yours,

Alex
0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
Ok, we're close!

This compiles fine, but it does not create/save a file (at all).
0
 
LVL 15

Expert Comment

by:simonet
Comment Utility
>but it does not create/save a file (at all).

Then there might be a problem with your saving routines. Debug the program ste-by-step to see what is going on and where the problem is.

Another option is to use ExpandFileName(FileName) in every saving routine you call. Example:

bmp.SaveToFile(ExpandFileName(savedialog1.filename));

Alex
0
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
I beleive that there is no need for ExpandFileName because the FileName property is full-qualified file name.

Motaz
0
 
LVL 15

Expert Comment

by:simonet
Comment Utility
I thought so too, Motaz. Perhaps there's also a change fibdev is looking for the saved file in the wrong folder.

Fibdev, please double-check the saving routines to see if they work. All the information I was supposed to give you, about the TSaveDialog control and handling filenames, was properly and correctly given. The point is, however, that TSaveDialog doesn't do the actual saving routine. The most it does is to return the Filter type, file name and a few other info that is irrelevant to the current issue.

Yours,

Alex
0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
Nope, it's not working.  I've been doing my test saves to the desktop and they are not there.
0
 
LVL 15

Expert Comment

by:simonet
Comment Utility
Can you test by saving them to somewhere else (like to "c:\TEMP") ???

Antoher thing:

You are doing this:
var
  bmp: TBitmap;


and then this:

     bmp.Assign(Image1.Picture.Bitmap);

There are 2 errors in that code:
#1 : You are defining an object, using, but never creating it:

If you declared an object and want to use, you must first call the CREATE constructor method and then, after you're done with it, call the FREE destructor. Here's an example:

var bmp : TBitmap;

begin
  bmp := TBitmap.create;
  try
    bmp.assign(image1.picture.bitmap);
    // do some other stuff with bmp
  finally
    bmp.free;
  end;
end;


#2: Another error: you are using an object just to save the bitmap to file. There's no need for that. You can simply use:

image1.picture.bitmap.SaveToFile(FileName);


Yours,

Alex
0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
Adjusted points from 50 to 75
0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
Alex,

Does this code set the value for the boolean variables?


procedure TForm1.SaveAs1Click(Sender: TObject);
type
  TPicFormat = (pfNone, pfBitmap, pfMetafile, pfJPEG, pfPhotoshop);
var

  PicFormat : TPicFormat;
  DoBitmap : Boolean;
  DoMetafile : Boolean;
  DoJPEG : Boolean;
  Ext : string;

begin
//  (snip)
  with SaveDialog1 do
  begin
     Filter := 'Bitmap|*.bmp|Metafile|*.wmf|JPEG|*.JPG';

if Execute then
begin
   if (ExtractFileExt(FileName)='') then
      case FilterIndex of
         1 : FileName := FileName + '.BMP';
         2 : FileName := FileName + '.WMF';
         3 : FileName := FileName + '.JPG';

      end;
      PicFormat  := TPicFormat(FilterIndex);
   end
else
   PicFormat := pfNone;


  If DoBitmap = True then begin
    TRY
     image1.picture.bitmap.SaveToFile(Savedialog1.FileName);
     statusbar1.Panels[1].Text := savedialog1.filename;
    FINALLY

    END;
    end;

    // Windows Metafile
   If DoMetafile = True then begin
    SaveDialog1.DefaultExt := 'wmf';
    TRY
    image1.picture.bitmap.SaveToFile(FileName);
    statusbar1.Panels[1].Text := savedialog1.filename;
    FINALLY

    END;
  END;
  end;
  end;

I was wondering if this is why it's not saving.  Everything seems to compile fine, but I can't see anywhere in the code where the values are assigned.

Is the default value of a boolean false?
0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
ok,

I got rid of the boolean values and got it to work like this:

If SaveDialog1.FilterIndex = 1 then begin
// save as bitmap stuff
end

If SaveDialog1.FilterIndex = 2 then
begin
// save metafile stuff
end

and so on...

Thank you for your help
Gabe
0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
I know you've helped me already and this question is done, but I'm having a error when I cancel the save dialog box with this code:

procedure TForm1.SaveAs1Click(Sender: TObject);
type
  TPicFormat = (pfNone, pfBitmap, pfMetafile, pfJPEG);
var

  PicFormat : TPicFormat;
  Ext : string;
  JPEG: TJPEGImage;

begin
//  (snip)
  with SaveDialog1 do
  begin
     Filter := 'Bitmap|*.bmp|Metafile|*.wmf|JPEG|*.JPG';

if Execute then
begin
   if (ExtractFileExt(FileName)='') then
      case FilterIndex of
         1 : FileName := FileName + '.BMP';
         2 : FileName := FileName + '.WMF';
         3 : FileName := FileName + '.JPG';

      end;
      PicFormat  := TPicFormat(FilterIndex);
   end
else
   PicFormat := pfNone;


  If SaveDialog1.FilterIndex = 1 then begin
    TRY
     image1.picture.bitmap.SaveToFile(Savedialog1.FileName);
     statusbar1.Panels[1].Text := savedialog1.filename;
    FINALLY

    END;
    end;

    // Windows Metafile
   If SaveDialog1.FilterIndex = 2 then begin
    SaveDialog1.DefaultExt := 'wmf';
    TRY
    image1.picture.bitmap.SaveToFile(FileName);
    statusbar1.Panels[1].Text := savedialog1.filename;
    FINALLY

    END;
  END;

   If SaveDialog1.Filterindex = 3 then begin
       JPEG := TJPEGImage.Create;
    TRY
      JPEG.CompressionQuality := 80;
      JPEG.Assign(Image1.Picture.Bitmap);
      JPEG.SaveToFile(savedialog1.filename);
      // set status bar to show the new filename
      statusbar1.panels[1].text := savedialog1.filename;
    FINALLY
      JPEG.Free
    END;
  END;
  end;
  end;


If you could help me, I would be thankful.

Gabe
0
 
LVL 15

Expert Comment

by:simonet
Comment Utility
Replace the first if statement for this:

if Execute then
begin
   if (ExtractFileExt(FileName)='') then
      case FilterIndex of
         1 : FileName := FileName + '.BMP';
         2 : FileName := FileName + '.WMF';
         3 : FileName := FileName + '.JPG';

      end;
      PicFormat  := TPicFormat(FilterIndex);
   end
else
   exit; // <<-- quit the rotine if user presses cancel.



Alex


0
 
LVL 3

Author Comment

by:fibdev
Comment Utility
Thanks Alex,

We're not worthy!  :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now