var ms:TMemoryStream; b:Array[0..1] of byte; s:string; begin ms:=TMemoryStream.Create; if self.OpenDialog1.Execute then begin ms.LoadFromFile( self.OpenDialog1.FileName ); ms.ReadBuffer( b,sizeof( b ) ); setlength( s,sizeof(b) ); move(b[0],s[1],sizeof(b) ); end; ms.Free;BMP图片b内容为BM JPG图片b内容为$FF $D8
BMP b: ($42, $4D)JPG b: ($FF, $D8)
建议新建一个字段,根据需要可选择tinyint,smallint或int,表示类型。
procedure TForm1.BitBtn1Click(Sender: TObject); var BmpImg:TBitmap; JpgImg:TJPEGImage; ms:TmemoryStream; PhothoType:string;//文件类型 begin { ADODataSet1.Active:=false; ADODataSet1.CommandText:='select * from employee where emplyee_id=:@Employee_id '; ADODataSet1.Parameters.ParamByName('@Employee_id').Value := edit1.Text; ADODataSet1.Active:=true; if ADODataSet1.IsEmpty then begin showmessage('em'); end; PhothoType := ADODataSet1.fieldByName('phothoType').AsString; ms := TmemoryStream.Create(); TBlobField(ADODataSet1.FieldByName('photho')).SaveToStream(ms); ms.Position := 0; if UpperCase(PhothoType)='.BMP' then begin BmpImg := TBitmap.Create(); BmpImg.LoadFromStream(ms); Image1.Picture.Assign(BmpImg); end else if UpperCase(PhothoType)='.JPG' then begin JpgImg := TJPEGImage.Create(); JpgImg.LoadFromStream(ms); Image1.Picture.Assign(JpgImg); end; } LoadPhotoFromDB( 'employee', 'emplyee_id', 'photho', edit1.Text, Image1); end;procedure TForm1.LoadPhotoFromDB(ATableName, KeyFieldName, PhotoFieldName, KeyValue: string; Img: TImage); var adsDataSet: TADODataSet; JpgImg:TJPEGImage; PhotoStream: TMemoryStream; begin PhotoStream := TMemoryStream.Create; try Img.Picture.Assign(nil); adsDataSet := TADODataSet.Create(nil); adsDataSet.Connection := ADOConnection1; with adsDataSet do try Active := False; CommandText := 'Select ' + PhotoFieldName + ' From ' + ATableName + ' Where ' + KeyFieldName + '=' + QuotedStr(KeyValue); Active := True; if (RecordCount > 0) then begin if TBlobField(FieldByName(PhotoFieldName)).BlobSize <= 0 then Exit; if not FieldByName(PhotoFieldName).IsBlob then Exit; TBlobField(FieldByName(PhotoFieldName)).SaveToStream(PhotoStream); PhotoStream.Position := 0; JpgImg := TJPEGImage.Create(); JpgImg.LoadFromStream(PhotoStream); Img.Picture.Assign(JpgImg); end else begin Img.Picture.Assign(nil); Exit; end; except raise Exception.Create('读取图片失败.'); end; finally FreeAndNil(adsDataSet); PhotoStream.Free; end; end;
看你具体的要求了
大俠具體怎麼寫,指點一下,謝謝
名称 宽度 标识符
BMP 2位 BM
JPG 10位 chr($FF)+chr($D8)+chr($FF)+chr($E0)+chr($00)+chr($10)+'JFIF'
GIF 3位 GIF
hongqi162(失踪的月亮)
這個代碼要怎麼寫呢,
ms:TMemoryStream;
b:Array[0..1] of byte;
s:string;
begin
ms:=TMemoryStream.Create;
if self.OpenDialog1.Execute then
begin
ms.LoadFromFile( self.OpenDialog1.FileName );
ms.ReadBuffer( b,sizeof( b ) );
setlength( s,sizeof(b) );
move(b[0],s[1],sizeof(b) );
end;
ms.Free;BMP图片b内容为BM
JPG图片b内容为$FF $D8
procedure TForm1.BitBtn1Click(Sender: TObject);
var
BmpImg:TBitmap;
JpgImg:TJPEGImage;
ms:TmemoryStream;
PhothoType:string;//文件类型
begin
{
ADODataSet1.Active:=false;
ADODataSet1.CommandText:='select * from employee where emplyee_id=:@Employee_id ';
ADODataSet1.Parameters.ParamByName('@Employee_id').Value := edit1.Text;
ADODataSet1.Active:=true; if ADODataSet1.IsEmpty then
begin
showmessage('em');
end; PhothoType := ADODataSet1.fieldByName('phothoType').AsString;
ms := TmemoryStream.Create();
TBlobField(ADODataSet1.FieldByName('photho')).SaveToStream(ms);
ms.Position := 0;
if UpperCase(PhothoType)='.BMP' then
begin
BmpImg := TBitmap.Create();
BmpImg.LoadFromStream(ms);
Image1.Picture.Assign(BmpImg);
end else if UpperCase(PhothoType)='.JPG' then
begin
JpgImg := TJPEGImage.Create();
JpgImg.LoadFromStream(ms);
Image1.Picture.Assign(JpgImg);
end; } LoadPhotoFromDB( 'employee', 'emplyee_id', 'photho', edit1.Text, Image1);
end;procedure TForm1.LoadPhotoFromDB(ATableName, KeyFieldName, PhotoFieldName, KeyValue: string;
Img: TImage);
var
adsDataSet: TADODataSet;
JpgImg:TJPEGImage;
PhotoStream: TMemoryStream;
begin
PhotoStream := TMemoryStream.Create;
try
Img.Picture.Assign(nil);
adsDataSet := TADODataSet.Create(nil);
adsDataSet.Connection := ADOConnection1;
with adsDataSet do
try
Active := False;
CommandText := 'Select ' + PhotoFieldName + ' From ' + ATableName
+ ' Where ' + KeyFieldName + '=' + QuotedStr(KeyValue);
Active := True;
if (RecordCount > 0) then
begin
if TBlobField(FieldByName(PhotoFieldName)).BlobSize <= 0 then
Exit;
if not FieldByName(PhotoFieldName).IsBlob then
Exit;
TBlobField(FieldByName(PhotoFieldName)).SaveToStream(PhotoStream);
PhotoStream.Position := 0; JpgImg := TJPEGImage.Create();
JpgImg.LoadFromStream(PhotoStream);
Img.Picture.Assign(JpgImg);
end
else begin
Img.Picture.Assign(nil);
Exit;
end;
except
raise Exception.Create('读取图片失败.');
end;
finally
FreeAndNil(adsDataSet);
PhotoStream.Free;
end;
end;