Re:TChart - Save chart image to MemoryStream
Hi Frans
Included in TeeChart 4.01 free upgrade (and Pro 4.01 trial)
there is an example about JPEG with the code below to
create a TJPEGImage using Delphi's Jpeg.dcu unit.
{ This function creates a TJPEGImage and draws a TeeChart on it }
Function GetChartJPEG(AChart:TCustomChart; Params:TJPEGDefaults):TJPEGImage;
var tmpBitmap:TBitmap;
begin
result:=TJPEGImage.Create; { <-- create a TJPEGImage }
tmpBitmap:=TBitmap.Create; { <-- create a temporary TBitmap }
try
tmpBitmap.Width :=AChart.Width; { <-- set the bitmap dimensions }
tmpBitmap.Height:=AChart.Height;
{ draw the Chart on the temporary Bitmap... }
AChart.Draw(tmpBitmap.Canvas,Rect(0,0,tmpBitmap.Width,tmpBitmap.Height));
{ set the desired JPEG options... }
With result do
begin
GrayScale :=Params.GrayScale;
ProgressiveEncoding :=Params.ProgressiveEncoding;
CompressionQuality :=Params.CompressionQuality;
PixelFormat :=Params.PixelFormat;
ProgressiveDisplay :=Params.ProgressiveDisplay;
Performance :=Params.Performance;
Scale :=Params.Scale;
Smoothing :=Params.Smoothing;
{ Copy the temporary Bitmap onto the JPEG image... }
Assign(tmpBitmap);
end;
finally
tmpBitmap.Free; { <-- free the temporary Bitmap }
end;
end;
Then you can call this more or less like this:
Var Params:TJPEGDefaults;
begin
Screen.Cursor:=crHourGlass;
try
{ Get the JPEG params }
With Params do
begin
GrayScale:=CheckBoxGrayScale.Checked;
ProgressiveEncoding:=CheckBoxProgressiveEncoding.Checked;
CompressionQuality:=TrackQuality.Position;
if RadioPixelFormat.ItemIndex=0 then PixelFormat:=jf24bit
else PixelFormat:=jf8bit;
ProgressiveDisplay:=CheckBoxProgressiveDisplay.Checked;
if RadioPerformance.ItemIndex=0 then Performance:=jpBestQuality
else Performance:=jpBestSpeed;
Case RadioGroupScale.ItemIndex of
0: Scale:=jsFullSize;
1: Scale:=jsHalf;
2: Scale:=jsQuarter;
3: Scale:=jsEighth;
end;
Smoothing:=CheckBoxSmoothing.Checked;
end;
{ Create the JPEG with the Chart image }
With GetChartJPEG(Chart1,Params) do
try
SaveToFile(ImageFileName); { <-- save the JPEG to disk }
Image1.Picture.LoadFromFile(ImageFileName); { <-- Load the JPEG }
finally
Free; { <-- free the temporary JPEG object }
end;
{ refresh the saved File Size }
LabelFileSize.Caption:=IntToStr(SizeFile(ImageFileName));
finally
Screen.Cursor:=crDefault;
end;
This code is in the Delphi 3 version of TeeChart Pro 4.01,
at the "teechart\examples\delphi3\jpeg" folder.
--
Regards
David Berneda
http://www.teemach.com
Quote
Frans Smit wrote in message <774cvg$g...@forums.borland.com>...
>Hi
>I would like to save the chart bmp image to a MemoryStream and then convert
>it to a jpeg or gif format whithout using the TChart.SaveToBitmapFile
method
>that saves it to a bmp file first. Can anyone that knows how to do this
>please help me.
>Thanks
>Frans