Solved

Get width and height of JPG-image from database

Posted on 2010-08-26
5
996 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…

752 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