How to save bitmap to a .bmp file?

I write an error when calling TBitmap.SaveToFile :

Project raised exception class EAccessViolation with message ...

Mistake!

PS Do not pay attention that the windows are colored, I just experimented with the system palette, but did not want to restore!

  • And method SaveToFile does not save unless? - DelphiM0ZG
  • In theory, it should, but I write an error, and it does not save. I do not know what the compiler needs? - delphikettle
  • @ DelphiM0ZG That's what I write! - delphikettle
  • I tried to write the file name just 'desktop.bmp' I also tried without FileCreate ('desktop.bmp'); - delphikettle
  • one
    My eyes! - karmadro4

2 answers 2

I just dialed this code in a quick way - everything is good for me.

 procedure TForm1.Button1Click(Sender: TObject); Var Bm: TBitmap; begin Bm:=TBitmap.Create(); Bm.Width:=400; Bm.Height:=300; Bm.Canvas.Brush.Color:=clRed; Bm.Canvas.FillRect(Rect(0, 0, 400, 300)); Bm.SaveToFile('bm.bmp'); Bm.Free; end; 

You freed the memory under Bitmap (Bm.Free;), even before you saved it. Take a closer look.

  • Oh, damn it, for sure !!! Thank you very much, I'll try it right now! - delphikettle
  • As an option, I can still suggest creating an Image programmatically and saving the Bitmap to an Image. And from Image, I think, just to be saved. - DelphiM0ZG
  • And how to save from Image, otherwise my image should be saved from Image, but I did not find the method I needed, and therefore I used Bitmap! - delphikettle
  • Everything worked out!!!! Thank you very much !!!! It remains only to learn how to manage windows, if I know the name of this process !!! - delphikettle
  • So save: Image.Picture.SaveToFile (SavePictureDialog.FileName + '. Bmp'); Then everything turned out? - DelphiM0ZG
 var b,bb: TBitmap; begin b:=TBitmap.Create; bb:=TBitmap.Create; try b.Width:=175; b.Height:=175; b.PixelFormat:=pf24bit; bb.Assign(MenuBackground.Picture.Graphic); StretchBlt(b.Canvas.Handle,0,0,175,175,bb.Canvas.Handle,0,0,175,175,SrcCopy); b.SaveToFile('save.bmp'); finally b.Free; bb.Free; 

end; end;