delphi Image控件操作数据库Image

来源:互联网 发布:实现发牌java 编辑:程序博客网 时间:2024/06/06 16:50


引用这个单元jpeg


procedure TCommMethods.uploadimage(img:TImage);
var
  OpenPictureDialog:TOpenPictureDialog;
  bmp1:TBitmap;
  jpg1:TJpegImage;
  sFileName:string;
begin
   OpenPictureDialog:= TOpenPictureDialog.Create(nil);
   with OpenPictureDialog do
   begin
      if   Execute   then
      begin
        sFileName:=FileName;
        if ExtractFileExt(sFileName)='.bmp'   then
        begin
          bmp1:=TBitmap.Create;
          bmp1.LoadFromFile(sFileName);
          bmp1.Assign(bmp1);
        end;
        if (ExtractFileExt(sFileName) ='.jpg') or  (ExtractFileExt(sFileName)='.jpeg') then
        begin
            bmp1:=TBitmap.Create;
            jpg1:=TJpegImage.Create;
            jpg1.LoadFromFile(sFileName);
            bmp1.Assign(jpg1);
        end;
        img.Picture.Assign(bmp1);
      end;
   end;
end;
用于插入数据库
function  TCommMethods.retrunchannelimageid(img:Timage):string;
var jpg1:TJpegImage;
begin
  try
    jpg1:=TJpegImage.Create;
    jpg1.Assign(img.Picture.Bitmap);
    with DM.qrypublic do
    begin

      Close;
      SQL.Clear;
      SQL.Add('INSERT INTO T_ImageOfChannel (channelimage) VALUES (:channelimage)');
      Parameters[0].Assign(jpg1);
      ExecSQL;

 

      Close;
      SQL.Clear;
      SQL.Add('SELECT max(channelimageid) as channelimageid FROM T_ImageOfChannel');
      Open;
      Result:=fieldbyname('channelimageid').AsString;
    end;
  except
    on Exception do
  end;
end;

用于更新数据库
Function TCommMethods.updateChannelImage(img:Timage;channelID:string):boolean;
var jpg1:TJpegImage;
begin
  try
    jpg1:=TJpegImage.Create;
    jpg1.Assign(img.Picture.Bitmap);
    with DM.qrypublic do
    begin

      Close;
      SQL.Clear;
      SQL.Add(' update T_ImageOfChannel  ');
      SQL.Add(' set channelimage = :channelimage  ');
      SQL.Add(' where channelimageid ='#39+channelID+#39'');
      Parameters[0].Assign(jpg1);
      ExecSQL;

    end;
    result:=true;
  except
    on Exception do result:=false;
  end;
end;


从数据库查询出来并在Image控件上面显示出来
procedure  TCommMethods.showchannelico(channelno:string;img:TImage);
var ipg1:TJPEGImage;
    bmp1:TBitmap;
begin
  try
    bmp1:=TBitmap.Create;
    ipg1:=TJPEGImage.Create;
    with DM.qrypublic do
    begin
      Close;
      SQL.Clear;
      SQL.Add('SELECT channelico FROM T_IcoOfChannel where Channelno='#39+channelno+#39'');
      Open;
      ipg1.Assign(FieldByName('channelico'));
      bmp1.Assign(ipg1);
      img.Picture.Assign(bmp1);
    end;
  except
    img.Picture.Assign(nil);
  end;
end;