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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 6
    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
     
     
    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.

     

    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...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek