Solved

Get width and height of JPG-image from database

Posted on 2010-08-26
5
984 Views
Last Modified: 2012-05-10
Hi,

Database field "Photo" contains a JPG-image (SQL Server 2008).
Field Photo's fieldtype = "Image"

Now I want to get the width and height of the stored image.
if Photo = NULL it will be: width = 0 and height = 0
else the actual width and height will be returned.

I prefer not to save the image to a file/stream, or place it in a TImage first. Unless there is no other possibility.
0
Comment
Question by:Delphiwizard
  • 2
  • 2
5 Comments
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 33539269
the simplest would be to have width and height column too in your database
and at the time of saving the image to the database,
also save the width and height

in Mike Lischke's library GraphicEx there is a function you can use for that:
it doesn't read the complete image, but it reads a part ...

you'll have to get the GraphicEx .pas files for this function to work
it's in his Jpg unit

http://www.soft-gems.net/index.php?option=com_content&task=view&id=13&Itemid=33
procedure GetJPEGInfo(Stream: TStream; var Width, Height: Cardinal);

var
  jc: TJPEGContext;

begin
  FillChar(jc, SizeOf(jc), 0);
  jc.err := jpeg_std_error;
  jc.common.err := @jc.err;
  jpeg_CreateDecompress(@jc.d, JPEG_LIB_VERSION, SizeOf(jc.d));
  try
    jpeg_stdio_src(@jc.d, Stream);
    jpeg_read_header(@jc.d, False);
    Width := jc.d.image_width;
    Height := jc.d.image_height;
  finally
    jpeg_destroy(@jc.common);
  end;
end;

Open in new window

0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 33542010
If I may ask, why do you not want to use the filestram capability of SQL Server 2008 and, instead, choose to bloat your database with the image in a column? ;-)
 
0
 

Author Comment

by:Delphiwizard
ID: 33542128
8080 diver:
Can you explain what it is you suggest?
0
 
LVL 22

Accepted Solution

by:
8080_Diver earned 500 total points
ID: 33542531
SQL Server 2008 has a capability to, in effect, treat the image as though it were in the database while it is actually managed via a file store.  This gives you the best of both worlds, so to speak.  The process is a little complicated to set up but, once set up, works rather neatly.  You need to look up the FILESTREAM option iun SS2008.  
If you put the image into a VarBinary(MAX) column in your database without the FILESTREAM option, it consumes largish amounts of space in a hury and complicates variaous other aspects of handling the images and data.  
However, if you put the image on a file store and then put the fully UNC pathed filename in the database, you open up a window for the image or the row that links to it being deleted wihtout them both being deleted.  That means you can have orphaned images (due to the row deletion) or broken links (due to the image deletion).
With the FILESTREAM image handling trick available in the SQL Server 2008 database, you can have the image automatically put on a file store with the link being automatically in the database.  If you delete the row in the database, you will delete the image as well.
Technically, I guess, you would still be putting the image into the SQL Server 2008 VarBinary(MAX) column; however the handling is far different.
0
 

Author Closing Comment

by:Delphiwizard
ID: 33544705
Thank you very much.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question