Overview
This is implemented via RegisterFileFormat which registers a new TGraphic class for use in LoadFromFile. You can then use the appropriate files with code similar to
SavePictureDialog1.Filter := GraphicFilter(TBitmap); OpenDialog1.Filter := GraphicFilter(TGraphic); |
All (*.jpg;*.jpeg;*.bmp;*.ico;*.emf;*.wmf) JPEG Image File (*.jpg) JPEG Image File (*.jpeg) Bitmaps (*.bmp) Icons (*.ico) Enhanced Metafiles (*.emf) Metafiles (*.wmf) |
Manipulating Pixels
var Form1: TForm1; Images: TList; procedure TForm1.Load_UIButtonClick(Sender: TObject); var p1:TPicture; i:integer; str1, str2, str3:String; b1:TBitmap; begin str1 :='E:\DelphiProjects\Images\Chab-01_'; str3 := '.jpg'; p1 := TPicture.Create; try images.Clear; for i := 1 to 9 do begin str2 := format('%2.2d', [i]); p1.LoadFromFile(str1+str2+str3); // E:\DelphiProjects\Images\Chab-01_03.JPG b1 := TBitmap.Create ; b1.Height := p1.Graphic.Height; // Must set Height & Width b1.Width := p1.Graphic.Width; b1.Canvas.Draw(0,0,p1.Graphic); images.Add(b1); end; Image1.Picture.Bitmap := b1; // Just indicates that the loop is done Image_UIScrollBar.SetParams(1, 1, images.Count); finally p1.Free; end; end; |
b1:=(TBitmap(images.Items [0])); x1 := trunc(x * image2.Width / b1.Width); y1 := trunc(y * image2.Height / b1.Height); Graph_x:= 1; Graph_y:= trunc((b1.Canvas.Pixels[x1, y1]and $ff)/4 +20 ); image2.Canvas.Pen.Color := clRed ; image2.Canvas.MoveTo(Graph_x, Graph_y); for i :=1 to images.Count-1 do begin b1 := (TBitmap(images.Items [i])); Graph_x:= 10 + x_spacing*i; Graph_y:= b1.Canvas.Pixels[x1, y1] ; Graph_y:= trunc((Graph_y and $ff)/4 + 20); image2.Canvas.LineTo(Graph_x, Graph_y); end; |
Save bitmap as JPEG file
The following example is strongly based on the help example for TJPEGImage.Assign (same as example for TGraphic.SaveToFile).
procedure Save_bitmap_as_jpeg_file_mc(bitmap: TBitmap; FileName: string); var jp: TJpegImage; //Requires the "jpeg" unit added to "uses" clause. ext : string; begin ext := ExtractFileExt(FileName) ; if SameText(ext, '.jpg') or SameText(ext, '.jpeg') then begin // code from help "TGraphic.SaveToFile - Create, Assign, SaveToFile example" jp := TJpegImage.Create; // default quality = 90 out of 100 try jp.Assign(Bitmap); jp.CompressionQuality := 100; // the maximum supported value jp.SaveToFile(FileName); finally jp.Free; end; end; end; |
Bogus JPEG Error #36
When writing JPEG files larger than 1 mb, you may get the bogus
One fix is to modify JPEG.pas and recompile. With Delphi 5 professional, the source code is located on the CD at
D:\Info\Extras\Jpeg\JPEG.PAS |
The problem is fixed by adding a try..except block and recompiling.
procedure TJPEGImage.Compress; try // this is the fix jpeg_finish_compress(jc.c); // This line generates the error except end; |
Though several web pages say different, this problem was observed in Delphi 6.
Author: Robert Clemenzi