首先看一下帮助:
TOpenPictureDialog displays a modal Windows dialog box for selecting and opening graphics files. This component is just like TOpenDialog, except that it includes a rectangular preview region. If the selected image can be read by TPicture, it is displayed in the preview region; supported file types include bitmap (.BMP), icon (.ICO), Windows metafile (.WMF), and enhanced Windows metafile (.EMF). If the selected image cannot be displayed, ?None)?appears in the preview region.If the user selects a file of an unrecognized type, TPicture raises an EInvalidGraphic exception.再看看TPicture:Use LoadFromFile to read a picture from disk. The TGraphic class created is determined by the file extension of the file. If the file extension is not recognized an EInvalidGraphic exception is raised.出错时正是EInvalidGraphic exception.所以说是因为TPicture不支持jpeg格式的图像。使用TJpegImage,就行了
unit jpegdemo;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, jpeg, ExtCtrls, FileCtrl, ComCtrls, Menus, printers;type
TForm1 = class(TForm)
Panel1: TPanel;
DirectoryListBox1: TDirectoryListBox;
FileListBox1: TFileListBox;
Panel3: TPanel;
DriveComboBox1: TDriveComboBox;
Scale: TComboBox;
PixelFormat: TComboBox;
ColorSpace: TComboBox;
Performance: TComboBox; ProgressiveDisplay: TCheckBox;
IncrementalDisplay: TCheckBox;
MainMenu1: TMainMenu;
File1: TMenuItem;
Open1: TMenuItem;
N1: TMenuItem;
Print1: TMenuItem;
PrinterSetup1: TMenuItem;
N2: TMenuItem;
Exit1: TMenuItem;
OpenDialog1: TOpenDialog;
PrinterSetupDialog1: TPrinterSetupDialog;
PrintDialog1: TPrintDialog;
ScrollBox1: TScrollBox;
Image1: TImage;
procedure FileListBox1DblClick(Sender: TObject); procedure SetJPEGOptions(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ProgressUpdate(Sender: TObject; Stage: TProgressStage;
PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string);
procedure Open1Click(Sender: TObject);
procedure Print1Click(Sender: TObject);
procedure PrinterSetup1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
procedure OpenFile(const Filename: string);
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.OpenFile(const Filename: string);
begin
try
Image1.Picture.LoadFromFile(Filename);
except
on EInvalidGraphic do
Image1.Picture.Graphic := nil; end;
SetJPEGOptions(self);
end;procedure TForm1.FileListBox1DblClick(Sender: TObject);
begin
OpenFile(FileListbox1.Filename);
end;procedure TForm1.SetJPEGOptions(Sender: TObject);
var
Temp: Boolean;
begin
Temp := Image1.Picture.Graphic is TJPEGImage;
if Temp then
with TJPEGImage(Image1.Picture.Graphic) do
begin
PixelFormat := TJPEGPixelFormat(Self.PixelFormat.ItemIndex); Scale := TJPEGScale(Self.Scale.ItemIndex);
Grayscale := Boolean(Colorspace.ItemIndex);
Performance := TJPEGPerformance(Self.Performance.ItemIndex);
ProgressiveDisplay := Self.ProgressiveDisplay.Checked;
end;
Scale.Enabled := Temp;
PixelFormat.Enabled := Temp;
Colorspace.Enabled := Temp;
Performance.Enabled := Temp;
ProgressiveDisplay.Enabled := Temp
and TJPEGImage(Image1.Picture.Graphic).ProgressiveEncoding; Image1.IncrementalDisplay := IncrementalDisplay.Checked;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Scale.ItemIndex := 0;
PixelFormat.ItemIndex := 0;
Colorspace.ItemIndex := 0;
Performance.ItemIndex := 0;
OpenDialog1.Filter := GraphicFilter(TGraphic);
FileListbox1.Mask := GraphicFileMask(TGraphic);
Image1.OnProgress := ProgressUpdate;
end;procedure TForm1.ProgressUpdate(Sender: TObject; Stage: TProgressStage; PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
const Msg: string);
begin
if Stage = psRunning then
Caption := Format('%d%%',[PercentDone])
else
Caption := 'Form1';
end;
procedure TForm1.Open1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
OpenFile(OpenDialog1.FileName);
end;procedure TForm1.Print1Click(Sender: TObject);var
AspectRatio: Single;
OutputWidth, OutputHeight: Single;
begin
if not PrintDialog1.Execute then Exit;
Printer.BeginDoc;
try
OutputWidth := Image1.Picture.Width;
OutputHeight := Image1.Picture.Height;
AspectRatio := OutputWidth / OutputHeight;
if (OutputWidth < Printer.PageWidth) and
(OutputHeight < Printer.PageHeight) then
begin
if OutputWidth < OutputHeight then begin
OutputHeight := Printer.PageHeight;
OutputWidth := OutputHeight * AspectRatio;
end
else
begin
OutputWidth := Printer.PageWidth;
OutputHeight := OutputWidth / AspectRatio;
end
end;
if OutputWidth > Printer.PageWidth then
begin
OutputWidth := Printer.PageWidth;
OutputHeight := OutputWidth / AspectRatio;
end;
if OutputHeight > Printer.PageHeight then begin
OutputHeight := Printer.PageHeight;
OutputWidth := OutputHeight * AspectRatio;
end;
Printer.Canvas.StretchDraw(Rect(0,0,
Trunc(OutputWidth), Trunc(OutputHeight)),
Image1.Picture.Graphic);
finally
Printer.EndDoc;
end;
end;procedure TForm1.PrinterSetup1Click(Sender: TObject);
begin
PrinterSetupDialog1.Execute;
end;procedure TForm1.Exit1Click(Sender: TObject);begin
Close;
end;end.
TOpenPictureDialog displays a modal Windows dialog box for selecting and opening graphics files. This component is just like TOpenDialog, except that it includes a rectangular preview region. If the selected image can be read by TPicture, it is displayed in the preview region; supported file types include bitmap (.BMP), icon (.ICO), Windows metafile (.WMF), and enhanced Windows metafile (.EMF). If the selected image cannot be displayed, ?None)?appears in the preview region.If the user selects a file of an unrecognized type, TPicture raises an EInvalidGraphic exception.再看看TPicture:Use LoadFromFile to read a picture from disk. The TGraphic class created is determined by the file extension of the file. If the file extension is not recognized an EInvalidGraphic exception is raised.出错时正是EInvalidGraphic exception.所以说是因为TPicture不支持jpeg格式的图像。使用TJpegImage,就行了
unit jpegdemo;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, jpeg, ExtCtrls, FileCtrl, ComCtrls, Menus, printers;type
TForm1 = class(TForm)
Panel1: TPanel;
DirectoryListBox1: TDirectoryListBox;
FileListBox1: TFileListBox;
Panel3: TPanel;
DriveComboBox1: TDriveComboBox;
Scale: TComboBox;
PixelFormat: TComboBox;
ColorSpace: TComboBox;
Performance: TComboBox; ProgressiveDisplay: TCheckBox;
IncrementalDisplay: TCheckBox;
MainMenu1: TMainMenu;
File1: TMenuItem;
Open1: TMenuItem;
N1: TMenuItem;
Print1: TMenuItem;
PrinterSetup1: TMenuItem;
N2: TMenuItem;
Exit1: TMenuItem;
OpenDialog1: TOpenDialog;
PrinterSetupDialog1: TPrinterSetupDialog;
PrintDialog1: TPrintDialog;
ScrollBox1: TScrollBox;
Image1: TImage;
procedure FileListBox1DblClick(Sender: TObject); procedure SetJPEGOptions(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ProgressUpdate(Sender: TObject; Stage: TProgressStage;
PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string);
procedure Open1Click(Sender: TObject);
procedure Print1Click(Sender: TObject);
procedure PrinterSetup1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
procedure OpenFile(const Filename: string);
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.OpenFile(const Filename: string);
begin
try
Image1.Picture.LoadFromFile(Filename);
except
on EInvalidGraphic do
Image1.Picture.Graphic := nil; end;
SetJPEGOptions(self);
end;procedure TForm1.FileListBox1DblClick(Sender: TObject);
begin
OpenFile(FileListbox1.Filename);
end;procedure TForm1.SetJPEGOptions(Sender: TObject);
var
Temp: Boolean;
begin
Temp := Image1.Picture.Graphic is TJPEGImage;
if Temp then
with TJPEGImage(Image1.Picture.Graphic) do
begin
PixelFormat := TJPEGPixelFormat(Self.PixelFormat.ItemIndex); Scale := TJPEGScale(Self.Scale.ItemIndex);
Grayscale := Boolean(Colorspace.ItemIndex);
Performance := TJPEGPerformance(Self.Performance.ItemIndex);
ProgressiveDisplay := Self.ProgressiveDisplay.Checked;
end;
Scale.Enabled := Temp;
PixelFormat.Enabled := Temp;
Colorspace.Enabled := Temp;
Performance.Enabled := Temp;
ProgressiveDisplay.Enabled := Temp
and TJPEGImage(Image1.Picture.Graphic).ProgressiveEncoding; Image1.IncrementalDisplay := IncrementalDisplay.Checked;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Scale.ItemIndex := 0;
PixelFormat.ItemIndex := 0;
Colorspace.ItemIndex := 0;
Performance.ItemIndex := 0;
OpenDialog1.Filter := GraphicFilter(TGraphic);
FileListbox1.Mask := GraphicFileMask(TGraphic);
Image1.OnProgress := ProgressUpdate;
end;procedure TForm1.ProgressUpdate(Sender: TObject; Stage: TProgressStage; PercentDone: Byte; RedrawNow: Boolean; const R: TRect;
const Msg: string);
begin
if Stage = psRunning then
Caption := Format('%d%%',[PercentDone])
else
Caption := 'Form1';
end;
procedure TForm1.Open1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
OpenFile(OpenDialog1.FileName);
end;procedure TForm1.Print1Click(Sender: TObject);var
AspectRatio: Single;
OutputWidth, OutputHeight: Single;
begin
if not PrintDialog1.Execute then Exit;
Printer.BeginDoc;
try
OutputWidth := Image1.Picture.Width;
OutputHeight := Image1.Picture.Height;
AspectRatio := OutputWidth / OutputHeight;
if (OutputWidth < Printer.PageWidth) and
(OutputHeight < Printer.PageHeight) then
begin
if OutputWidth < OutputHeight then begin
OutputHeight := Printer.PageHeight;
OutputWidth := OutputHeight * AspectRatio;
end
else
begin
OutputWidth := Printer.PageWidth;
OutputHeight := OutputWidth / AspectRatio;
end
end;
if OutputWidth > Printer.PageWidth then
begin
OutputWidth := Printer.PageWidth;
OutputHeight := OutputWidth / AspectRatio;
end;
if OutputHeight > Printer.PageHeight then begin
OutputHeight := Printer.PageHeight;
OutputWidth := OutputHeight * AspectRatio;
end;
Printer.Canvas.StretchDraw(Rect(0,0,
Trunc(OutputWidth), Trunc(OutputHeight)),
Image1.Picture.Graphic);
finally
Printer.EndDoc;
end;
end;procedure TForm1.PrinterSetup1Click(Sender: TObject);
begin
PrinterSetupDialog1.Execute;
end;procedure TForm1.Exit1Click(Sender: TObject);begin
Close;
end;end.
解决方案 »
- 急:高分悬赏:关于TMS 组建中 advstringgrid 着色换行的问题
- 求助--在线等----从字元字串转换到datetime时,转换失败
- Assigned问题?
- 大家说说用什么工具写帮助文档最方便快捷?且维护简单。
- 我的数据库里有一个boolean字段.
- 光标的设置?(delphi5.0)
- 第四封贴,>>>>>>>>>地狱情人,请进>>>>>>>>>>>>>>>>>>>>>>
- 50分,一個簡單得不能再簡單的問題,可是就是有人做不出……:)
- fastreport强制换页的问题(急啊)
- 怎样实现这个用DELPHI2就能实现的功能,大家帮忙拉
- delphi如何接收VC写的DLL里的形参的值?
- 怎样通过点击图标来实现另外一个可执行程序执行? 急!急!
它支持jpg,gif等图片格式