Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 5 - Saving Images and Binary Files to a Databa...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Saving Images and Binary Files to a Database with Delphi 2
By: Danish Ahmed
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-07-03

    Table of Contents:
  • Saving Images and Binary Files to a Database with Delphi 2
  • Using Streams to save binaries
  • Retrieving BLOB data and saving to file
  • Checking the First 4 Bytes or Magic Numbers
  • Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.

     

    DELPHI-KYLIX ARTICLES

    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...
    - Using the Client Dataset in File-Based Archi...
    - Demystifying the Client Dataset
    - Working with INI Files in Delphi
    - Creating Data Link (UDL) Files in Delphi
    - Looking at the Details for an Invoicing Appl...
    - Invoicing in Delphi: Show Me the Money
    - Saving Images and Binary Files to a Database...
    - Saving Files to a Database using Delphi: Sav...
    - Creating CF Applications and Integrating a S...
    - Using Try and Finally to Help Prevent Memory...
    - The Implementation of an FTP Server
    - FTP Server: The Theory







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway