Solved

Get width and height of JPG-image from database

Posted on 2010-08-26
5
973 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now