一个图象存取的问题 ( 积分: 30 )

  • 主题发起人 主题发起人 lpzland
  • 开始时间 开始时间
L

lpzland

Unregistered / Unconfirmed
GUEST, unregistred user!
各位老大,我使用一个OpenPictureDialog调取图片,虽然在它的Filter属性中包含了jpg和jpeg格式,但当我运行下面这段程序的时候:
procedure TForm1.Button1Click(Sender: TObject);
begin
if openpicturedialog1.Execute then
image1.Picture.LoadFromFile(openpicturedialog1.FileName );
end;
虽然跳出了选择对话框,但只要选择jpg格式的图片,系统就报错为没有jpg这种格式,不知道这是什么问题啊,请各位老大帮忙看看!
 
各位老大,我使用一个OpenPictureDialog调取图片,虽然在它的Filter属性中包含了jpg和jpeg格式,但当我运行下面这段程序的时候:
procedure TForm1.Button1Click(Sender: TObject);
begin
if openpicturedialog1.Execute then
image1.Picture.LoadFromFile(openpicturedialog1.FileName );
end;
虽然跳出了选择对话框,但只要选择jpg格式的图片,系统就报错为没有jpg这种格式,不知道这是什么问题啊,请各位老大帮忙看看!
 
图象可以取了,但存不进去,不知道老大能否给一个标准的图象存取方案啊!
 
Images1.picture.savefilename
 
各位老大,我用了以下程序来存取图象,但程序运行到下面打“$$$$$$$$$”号的地方就不能运行了。我用的是table来存取数据的,请各位老大帮我看看我哪些语句有问题了,谢谢!

procedure TForm1.Button1Click(Sender: TObject);
begin
if openpicturedialog1.Execute then
image1.Picture.LoadFromFile(openpicturedialog1.FileName );
end;

procedure TForm1.Button2Click(Sender: TObject);
var
strm:tmemorystream;
ext:string;
begin
if image1.picture.Graphic <> nil then //避免image1中无图像保存出错
begin
ext:=extractfileext(openpicturedialog1.FileName ); //取出文件的扩展名
strm := tmemorystream.Create ;
try
image1.Picture.Graphic.SaveToStream(strm);
table1.Edit ;
strm.Position :=0;
tblobfield(table1.FieldByName('a')).LoadFromStream(strm);
//如需直接由文件保存可采用如下注释行
TBlobField(table1.FieldByName('a')).LoadFromFile(OpenPictureDialog1.FileName);
//以下记录保存到数据库的图像格式
if uppercase(ext) = '.BMP' then
table1.FieldByName('isbmp').Value := 1 //BMP型图像数据
else if (uppercase(ext) = '.JPG') OR ( uppercase(ext) = '.JPEG') Then
table1.FieldByName('isbmp').Value := 0; //JPEG型图像数据
table1.Post ;
finally
strm.Free ; //笔者发现如strm采用tblobstream类,程序运行到该语句会出现问题
end;
end;
end;

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
var
strm:tadoblobstream;
jpegimage:TJPEGImage;
//jpegimage:TJPEGImage;
bitmap:tbitmap;
begin

strm := tadoblobstream.Create(tblobfield(table1.fieldbyname('a')),bmread); ///// $$$$$$$$$
strm := table1.Create(tblobfield(table1.fieldbyname('a')));

try //try1
strm.position :=0;
image1.Picture.Graphic := nil; //清除图像
// BMP、JPEG两种图像数据必需分别处理
if table1.fieldbyname('isbmp').asstring ='1' then //BMP型图像数据
begin //begin11
bitmap := tbitmap.Create ;
try //try11
bitmap.LoadFromStream(strm);
image1.Picture.Graphic := bitmap;
finally
bitmap.Free;
end; //end try11
end //end begin11
else if table1.fieldbyname('isbmp').asstring ='0' then //JPEG型图像数据
begin //begin12
jpegimage := tjpegimage.Create ;
try //try12
jpegimage.LoadFromStream(strm);
image1.Picture.Graphic := jpegimage;
finally
jpegimage.Free ;
end; //end try12
end; //end begin12
finally
strm.Free ;
end; //end try1
end;
 
var
JPEGImage: TJPEGImage;
MS: TMemoryStream;

with cdsCardHolder do
begin

MS := TMemoryStream.Create;
try
if ImagePhoto.Picture.Graphic is TBitmap then//Is Bitmap file?convert to Jpeg
begin
JPEGImage := TJpegImage.Create;
try
Bmp2Jpg(ImagePhoto.Picture.Bitmap, JPEGImage, 65);
JPEGImage.SaveToStream(MS);
finally
JPEGImage.Free;
end;
end
else //Is Jpeg file?
TJPEGImage(ImagePhoto.Picture.Graphic).SaveToStream(MS);
MS.Seek(0, soFromBeginning);
TBlobField(FieldByName('PHOTO')).LoadFromStream(MS);
finally
MS.Free;
end;

Post;
end;


procedure Bmp2Jpg(Bmp: TBitmap; JpegImage: TJpegImage; Quality: Integer = 100);
begin
if Assigned(Bmp)
then begin
JPEGImage.Assign(Bmp); //It′s all folks...
JPEGImage.CompressionQuality := Quality;
JPEGImage.JPEGNeeded; //Key method...
JPEGImage.Compress;
end;
end;
 
同意二楼
 
我是这样做的:
先声明一个转换函数:
function GraphicToBitmap(P :TPicture) :TBitmap;
begin
Result :=TBitmap.Create;
with Result do
begin
Width :=P.Width;
Height :=P.Height;
Canvas.Draw(0,0,P.Graphic);
end;
end;
再用语句:
if graphictobitmap(image1.Picture)<>nil then
table1.Fields[9].Assign(GraphicToBitmap(image1.Picture))
else
table1.Fields[9].Assign(nil);
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部