数据库中的图片数据怎样在Image中显示
procedure TSpczhForm.SpeedButton1Click(Sender: TObject);
begin
OpenPictureDialog1.Execute; 
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName); 
end;procedure TSpczhForm.SaveSBClick(Sender: TObject);
var
Graphic1:TImage;
begin
Graphic1 := TImage.Create(self); 
Graphic1.Picture.LoadFromFile(OpenPictureDialog1.FileName); 
FieldByName('sptp').Assign(Graphic1.Picture); 
Graphic1.Free; 
end;
以上是我往数据库中添加图片的代码,不知有没有更好的办法。
现在我想把它反过来,当单击DBGrid中某一条记录时,把数据库中的图片数据在Image控件中显示出来以便可以进行修改操作,我现在脑子已经一片混乱,希望各位仁兄指点一二!小弟不胜感激!!!

解决方案 »

  1.   

    to yoic(我是一棵菠菜,菜,菜,菜,菜,菜,菜)  我是老白菜!
      

  2.   

    首先,用户可能往数据库里存的是BMP或者JPG,或者其他的一些格式的图片。取的时候要注意。
    有几个办法,第一,插入的时候就将文件的格式记录在一个字段里,取的时候用的着。
    第二,判断几种主流的文件格式,也就是BMP或者JGP,要知道,这两种格式的文件不加判断是会出错的。
    下面是第二种方法的代码:
    var
      JPEG:TJPEGImage;
      BMP:TBitmap;
      MS:TMemoryStream;
    Begin
      try
        MS:=TMemoryStream.Create;
        TBlobfield(ADODataSet1.FieldByName('IPicture')).SaveToStream(MS);
        MS.Position:=0;    if MS.Size>0 then //得有图片才行呀
          try
            Jpeg:=TJPEGImage.Create;
            Jpeg.LoadFromStream(MS);
            Image1.Picture.Graphic := Jpeg;
            Jpeg.Free;
          except
            Jpeg.Free;
            try
              BMP:=TBitmap.Create;
              MS.Position:=0;
              BMP.LoadFromStream(MS);
              Image1.Picture.Graphic:=nil;
              Image1.Picture.Graphic:= BMP;
              BMP.Free;
            except
              BMP.Free;
              Application.MessageBox('载入图片信息出错,可能图片格式不为JPEG或者BMP','提示',MB_OK OR MB_ICONINFORMATION);
            end;
          end;
      finally
        MS.Free;
      end;
    End;
    代码是我截取出来的,你看看自己改一改吧。
    有些地方可能不太聪明,你看看别人有什么方法可以借鉴。
      

  3.   

    加jpeg单元
    var   MS_JpegStream:TMemoryStream;
      M_BitMap:TBitMap;
      M_Jpeg:TJpegImage; begin
                MS_JpegStream:=TMemoryStream.Create;            
                (adoquery1.FieldByName('h_photo') as tblobfield).savetostream(MS_JpegStream);
                image1.Picture.Graphic:=nil;
                image1.Picture.Graphic:=TJpegImage.Create;
                MS_JpegStream.Position:=0;
                image1.Picture.Graphic.LoadFromStream(MS_JpegStream);  
    end;