Home arrow Delphi-Kylix arrow Page 5 - Saving Images and Binary Files to a Database with Delphi 2
DELPHI-KYLIX

Saving Images and Binary Files to a Database with Delphi 2


In my previous article I talked about ADO Components and how to use them to conduct database transactions from our application. We created a small application to save bitmap files to a database and retrieve them using the data-aware TDBImage component. In this article you'll learn how to save other types of files.

Author Info:
By: Danish Ahmed
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
July 03, 2007
TABLE OF CONTENTS:
  1. · Saving Images and Binary Files to a Database with Delphi 2
  2. · Using Streams to save binaries
  3. · Retrieving BLOB data and saving to file
  4. · Checking the First 4 Bytes or Magic Numbers
  5. · Code

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Saving Images and Binary Files to a Database with Delphi 2 - Code
(Page 5 of 5 )

In the code below are some of the magic numbers of popular file formats I found while playing around. They work fine for me but if the I missed something do let me know.

Here is the full code:

procedure TForm1.Button1Click(Sender: TObject);
var
 iPKey:integer;
 MStream:TMemoryStream;
 Sig1,Sig2:  WORD;  //16-bit unsigned integer  = 2 bytes
begin
 iPKey:=StrToInt(ediPKey.Text);
 ADODataSet1.Connection:=ADOConnection1;
 ADODataSet1.CommandText:='Select * From lmgTbl Where PKey=:param0';
 ADODataSet1.Parameters.ParamByName('param0').Value:=iPKey;
 ADODataSet1.Open;
 MStream:=TMemoryStream.Create;
 try
 TBlobField(ADODataSet1.FieldByName('Image')).SaveToStream(MStream);
    MStream.Seek(0, soFromBeginning);
    MStream.Read(Sig1,2);
    MStream.Position := 2;
    MStream.Read(Sig2,2);
    if (Sig1=$4D42) then  // little Endian format of the first 2 bytes of bitmap
     begin
       MStream.SaveToFile('Img'+IntToStr(iPKey)+'.bmp');
       ShowMessage('File saved as Bitmap');
     end
     else
      //ShowMessage('File is not Bitmap');
      if (Sig1 = $D8FF) and (Sig2 = $E0FF) then // little Endian format of the first 4 bytes of  JPEG
      begin
        MStream.SaveToFile('Img'+IntToStr(iPKey)+'.jpg');
        ShowMessage('File saved as JPEG');
      end
      else
       if (Sig1 = $4947) and (Sig2 = $3846) then // little Endian format of the first 4 bytes of GIF
        begin
        MStream.SaveToFile('Img'+IntToStr(iPKey)+'.gif');
        ShowMessage('File saved as GIF');
        end
         else
         if (Sig1 = $5025)and (Sig2 = $4644) then // // little Endian format of the first 4 bytes of PDF
         begin
        MStream.SaveToFile('Doc'+IntToStr(iPKey)+'.pdf');
        ShowMessage('File saved as PDF');
         end
         else
          if (Sig1 = $CFD0) and (Sig2 = $E011) then // // little Endian format of the first 4 bytes of DOC
        begin
        MStream.SaveToFile('Doc'+IntToStr(iPKey)+'.doc');
        ShowMessage('File saved as DOC');
        end
        else
          if (Sig1 = $4B50) and (Sig2 = $0403)  then // // little Endian format of the first 4 bytes of ZIP
        begin
        MStream.SaveToFile('Archive'+IntToStr(iPKey)+'.zip');
        ShowMessage('File saved as ZIP');
        end
         else
         begin        // unrecognized
        MStream.SaveToFile('File'+IntToStr(iPKey)+'.dat');
        ShowMessage('Unrecognized file format! '+#13+'saved with .dat extension');
        end;
     finally
  MStream.Free;
 end;
end;
end;

Danish Ahmed
Mindfire Solutions


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials