Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!agate!bionet!apple!rutgers!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!sm.unisys.com!csun!polyslo!dorourke From: dorourke@polyslo.CalPoly.EDU (David M. O'Rourke) Newsgroups: comp.sys.mac.programmer Subject: Re: Saving a picture to a file Keywords: PICT QuickDraw Message-ID: <5532@polyslo.CalPoly.EDU> Date: 8 Nov 88 19:38:20 GMT References: Reply-To: dorourke@polyslo.CalPoly.EDU (David M. O'Rourke) Organization: Cal Poly State University -- San Luis Obispo Lines: 117 In article elkins@topaz.rutgers.edu (George Elkins) writes: > >Does anyone out there have any code examples of saving a QuickDraw >picture to a PICT file? (instead of merely drawing it on the screen) >I am trying to learn how to do this, and a short code example >would greatly help me. > >Thanks, >George Elkins function WritePicture (thePictHandle : PicHandle): BOOLEAN; var FileErr : OSErr; PictLength : LONGINT; LoopIndex, ZeroValue, FileRefNum : INTEGER; FileRecord : SFReply; PutFilePoint : Point; FileFndrInfo : FInfo; begin {First we need to get a File Name from the user. To do this we're going} {to use the standard file package from Volume I of inside Mac. I have put constants in some position that should be variables, please extend this code to calculate screen position, prompt, ect. at run time, for now this illustrates the concept} PutFilePoint.v := 30; PutFilePoint.h := 50; {This value really should be calculated based on the screen size from ScreenBits.bounds} SFPutFile(PutFilePoint, 'Please type a file Name', 'PictFile.pict', nil, FileRecord); {Now that we have an output file name, and record lets use it} FileErr := GetFInfo(FileRecord.fName, FileRecord.vRefNum, FileFndrInfo); if FileErr = fnfErr then begin FileErr := Create(FileRecord.fName, FileRecord.vRefNum, YourCreator, 'PICT'); if FileErr <> noErr then begin Do_Something_To_Report_the_problem; end; {if FileErr <> noErr} end {if FileErr = fnfErr} else begin FileFndrInfo.Type := 'PICT'; FileErr := SetFInfo(FileRecord.fName, FileRecord.vRefNum, FileFndrInfo); end; {else} FileErr := FSOpen(FileRecord.fName, FileRecord.vRefNum, FileRefNum); if FileErr <> noErr then begin Do_Something_To_Report_the_problem; end; {if FileErr <> noErr} FileErr := SetFPos (FileRefNum, fsFromStart, 0); if FileErr <> noErr then begin Do_Something_To_Report_the_problem; end; {if FileErr <> noErr} {for Some reason we need to put 512 16-bit zeros on the front of the file, I don't know why, but here is some crude code to do this} ZeroValue := 0; for LoopIndex := 0 to 511 do begin FileErr := FSWrite(FileRefNum, SIZEOF(Integer), @ZeroValue); if FileErr <> noErr then begin Do_Something_To_Report_the_problem; end; {if FileErr <> noErr} end; {for LoopIndex := 0 to 511} PictLength := GetHandleSize(Handle(thePictHandle)); HLock(Handle(thePictHandle)); FileErr := FSWrite(FileRefNum, PictLength, @thePictHandle^^); if FileErr <> noErr then begin Do_Something_To_Report_the_problem; end; {if FileErr <> noErr} FileErr := SetEOF(FileRefNum, PictLength); if FileErr <> noErr then begin Do_Something_To_Report_the_problem; end; {if FileErr <> noErr} HUnLock(Handle(thePictHandle)); FileErr := FSClose(FileRefNum); if FileErr <> noErr then begin Do_Something_To_Report_the_problem; end; {if FileErr <> noErr} if FileErr = noErr then WritePicture := True else WritePicture := False; {Set the value of the function to indicate success/falure} end; {WritePicture} This codes isn't perfect, but it should give you a rough idea about how to approach the problem. As far as readings go I'd recommend that you check chapter 20 of vol I, chap. 5 of volume 1, and chap. 4 in vol II, those should help the matter some what. Hope it helps -- David M. O'Rourke dorourke@polyslo.calpoly.edu "If it doesn't do Windows, then it's not a computer!!!" Disclaimer: I don't represent the school. All opinions are mine!