delphi 图片保存,装载

2019-04-15 13:55发布

TABLE1:STUDENT ,图片的字段为PIC,关键字段NUM 有2个数据集TAdoquery. ADOQuery1 .sql.text:='select * from student' ADOPIC.sql.text:='select  num,pic  from student where num =:num' -----------------------------------------装载图片 function TForm1.LoadPicture: Boolean;
var
  tempstream        : TStream;
  tempjpeg          : TJPEGImage;
begin
  Image1.Picture := nil;   with ADOPIC do
  begin
    close;
    Parameters[0].Value := ADOQuery1.Fieldbyname('num').AsString;
    open;
  end;
  if ADOPIC.FieldByName('pic').ASString <> '' then
  begin
    tempstream := TStringStream.Create(' ');
    tempjpeg := TJPEGImage.Create;
  try
    TBlobField(ADOPIC.FieldByName('pic')).SaveToStream(tempstream);
    tempstream.Position := 0;
    tempjpeg.LoadFromStream(tempstream);
    Image1.Picture.Bitmap.Assign(tempjpeg);
    tempstream.Free;
    tempjpeg.Free;
  except
    ShowMessage('装载图样一失败,请检测图片格式。');
    tempstream.Free;
    tempjpeg.Free;
  end;
  end
end;   -----------------------------------保存图片 function TForm1.savepic: Boolean;
var
Myjpeg            : TJPEGImage;
MS                : TMemoryStream;
begin
  ADOPIC.Open;   if (ADOQuery1.FieldByName('num').AsString = '') then
  begin
    ShowMessage('学号不能为空值');
    Abort;
  end;
  ADOPIC.Open;
  MyJPEG := TjpegImage.Create;
  try
    if Image1.Picture.Graphic <> nil then
    with MyJPEG do
    begin
      Assign(Image1.Picture.Graphic);
      MS := TMemoryStream.create;
      SaveToStream(MS);
      MS.Position := 0;
      with ADOPIC do
      begin
        close;
        Parameters[0].Value := ADOQuery1.FieldByName('NUM').AsString;
        open;
        Edit;
        TBlobField(FieldbyName('pic')).LoadFromStream(MS);
        Post;
        UpdateBatch;
      end;
    end
    else
    with MyJPEG do
    begin
      with ADOPIC do
      begin
          close;
          Parameters[0].Value := ADOQuery1.FieldByName('NUM').AsString;;
          open;
          edit;
          FieldbyName('pic').Value := null;
          Post;
          UpdateBatch;
      end;
    end;
    showmessage('保存图片一一成功!');
    MyJPEG.Free;
  except
    showmessage('保存图片一一失败,可能图片的格式有问题');
    MyJPEG.Free;
    exit;
  end; end;