delphi中图片保存后再使DBIMAGE显示总提示‘BITMAP IMAGE IS NOT VOLID'

时间:2022-10-24 09:05:46
各位大佬:
小弟新手,编个条形打开小程序,先在IMAGE1中产生条形码,再存入个文件取名’TEST.BMP',然后,再读入IMAGE2中(因不知在IMAGE1中产生的条形码图形是否是图像),再保存在SQL中。
再次通过DBIMAGE控件显示,可是总是提示‘BITMAP IMAGE IS NOT VOLID'信息,不知为何,请各位大佬指教,万分感谢。
另外数据库是SQL2000,保存图像的字段名为TS,值为IMAGE
unit Unit1;

interface

uses
  
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, jpeg, Menus, Spin, DB, DBTables, ADODB;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Panel1: TPanel;
    Panel2: TPanel;
    CheckBox1: TCheckBox;
    Edt_CHeight: TEdit;
    Edt_CWidth: TEdit;
    Edt_CWidthShort: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Edt_Size: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Edit1: TEdit;
    ComboBox1: TComboBox;
    Label7: TLabel;
    Label8: TLabel;
    Edit2: TEdit;
    Label9: TLabel;
    Edit3: TEdit;
    Label10: TLabel;
    Edit4: TEdit;
    Label11: TLabel;
    Edt_CodeStr: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Image1: TImage;
    PopupMenu1: TPopupMenu;
    CBX_CodeMode: TComboBox;
    Edt_Corner: TSpinEdit;
    ScrollBox1: TScrollBox;
    Image2: TImage;
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    procedure EDT_CODESTRKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  function BuildCodes(Handle:THandle;CodeStr:string;CodeType:integer;CodeTittle:integer;Corner:integer;
           CHeight:integer;CWidth:integer;CWidthShort:integer;
           CTextOutSize:integer; var CodeCanvas:Timage):integer; stdcall;  external 'ZJCode.dll' ;
var
  Form1: TForm1;
  MyCodeStr:string;
  MyCodeType:integer;
  MyCodeTittle:integer;
  MyCorner:integer;
  MyCheight:integer;
  MyCWidth:integer;
  MyCWidthShort:integer;
  MyCanvas:Tcanvas;
  MyCTextOutSize:integer;
  MyBitmap:TBitmap;
  s:string;
implementation
uses
  BuildCode;
{$R *.dfm}
  
procedure TForm1.EDT_CODESTRKeyPress(Sender: TObject; var Key: Char);
begin
      if key=#13 then
   button1.Click;
end;

procedure TForm1.Button1Click(Sender: TObject);(在IMAGE1中生成条形码并保存图像文件TEST,IMAGE2中读入TEST)
begin
 MyCodeStr:=Edt_CodeStr.Text;
 MyCodeType:=CBX_CodeMode.ItemIndex;
  MyCodeTittle:=1;
  MyCorner:=Edt_Corner.Value;
  MyCheight:=strtoint(Edt_CHeight.text);
  MyCWidth:=strtoint(Edt_CWidth.text);
  MyCWidthShort:=strtoint(Edt_CWidthShort.text);
  MyCTextOutSize:=strtoint(Edt_Size.text);

  Image1.Free;
 Image1:=TImage.Create(ScrollBox1);
  Image1.Parent:=ScrollBox1;
  Image1.PopupMenu:=PopupMenu1;
  Image1.Stretch:=True;
 if CheckBox1.Checked then
  MyCodeTittle:=0 else MyCodeTittle:=1;

  BuildCodes(Application.Handle,MyCodeStr,MyCodeType,MyCodeTittle,MyCorner,MyCHeight,MyCWidth,MyCWidthShort,MyCTextOutSize,image1);//生成条形码
   image1.Picture.SaveToFile('e:\tm_japan\条形码\test.bmp');
   image2.Picture.LoadFromFile('e:\tm_japan\条形码\test.bmp');
  end;

procedure TForm1.FormCreate(Sender: TObject);//生成条形码的大小等数据的初始值
begin
  Edt_CodeStr.Text:='';
  Edt_Corner.Text:='0';
  Edt_CHeight.Text:='100';
  Edt_CWidth.Text:='9';
  Edt_CWidthShort.Text:='3';
  Edt_Size.Text:='9';
  edit1.Text:='';
  edit2.Text:='';
  edit3.Text:='';
  edit4.Text:='';
  combobox1.Text :='';
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button2Click(Sender: TObject);

  Var
  MyJPEG : TJPEGImage;
  MS: TMemoryStream;
Begin
  MyJPEG := TJPEGImage.Create;
  Try
    With MyJPEG do
   Begin
     Assign(Image2.Picture.BITMAP);
    MS:=TMemoryStream.create;
    SaveToStream(MS);
    MS.Position := 0;
    
    ADOtable1.Append;
    ADOTable1.Edit;
    TBlobField(ADOTable1.FieldbyName('TS')).LoadFromStream(MS);
    ADOTable1.Post;
    Messagebox(Getactivewindow(), '图像保存完毕!', '保存', MB_OK);
   End;
  Finally
   MyJPEG.Free;
  End;
End;




end.

3 个解决方案

#1


BMP的还用TJPEGImage干麻.

    ms:= TMemoryStream.Create;
    Image2.Graphic.SaveToStream(ms);   // 直接IMAGE2
    ms.Position:= 0;
    ADOQ1.Edit;
    TBlobField(ADOQ1.Fields[0]).LoadFromStream(ms);
    ADOQ1.Post;
    ms.Free;

#2


procedure TForm1.ButtonSaveImgClick(Sender: TObject);
TS: TMemoryStream;
  LSI: TLSImg;
begin
 with ADOTable1 do
 begin
  if not Eof then
  begin
   LSI := TLSImg.Create;
   TS := TMemoryStream.Create;
   (Fields[1] as TBlobField).SaveToStream(TS);
   LSI.LoadFromStream(TS);
   Image1.Picture := LSI.Picture;
   TS.Free;
   LSI.Free;
  end;
 end;
end;

#3


万分感谢二位大佬

#1


BMP的还用TJPEGImage干麻.

    ms:= TMemoryStream.Create;
    Image2.Graphic.SaveToStream(ms);   // 直接IMAGE2
    ms.Position:= 0;
    ADOQ1.Edit;
    TBlobField(ADOQ1.Fields[0]).LoadFromStream(ms);
    ADOQ1.Post;
    ms.Free;

#2


procedure TForm1.ButtonSaveImgClick(Sender: TObject);
TS: TMemoryStream;
  LSI: TLSImg;
begin
 with ADOTable1 do
 begin
  if not Eof then
  begin
   LSI := TLSImg.Create;
   TS := TMemoryStream.Create;
   (Fields[1] as TBlobField).SaveToStream(TS);
   LSI.LoadFromStream(TS);
   Image1.Picture := LSI.Picture;
   TS.Free;
   LSI.Free;
  end;
 end;
end;

#3


万分感谢二位大佬