Solved

Get width and height of JPG-image from database

Posted on 2010-08-26
5
1,002 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 38

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

632 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