PC Review


Reply
Thread Tools Rate Thread

How do I get the file size of a JPEG and resize it if too big?

 
 
Karen Hill
Guest
Posts: n/a
 
      29th Dec 2006
How do I get the file size of a JPEG and resize it if it is too big in
terms of MB or KB? I am using Windows Forms, .NET 2.0, C#. I want to
put this image in a database so I want to make sure it isn't too large
(ideally no more than 200K. I tried discovering the size of a JPEG
using sizeof but that only works with built in types like int and long.


regards,

karen

 
Reply With Quote
 
 
 
 
=?Utf-8?B?cm9nZXJfMjc=?=
Guest
Posts: n/a
 
      29th Dec 2006
if I'm not mistaken you need to use the fileinfo object,

the code should be similar to the code below, but it is not correct because
of casing.

FileInfo fi = new FileInfo(path of file)
then
long filesize = fi.FileSize()

if(filesize> 1,000,000) //1MB
{
whatever
}


"Karen Hill" wrote:

> How do I get the file size of a JPEG and resize it if it is too big in
> terms of MB or KB? I am using Windows Forms, .NET 2.0, C#. I want to
> put this image in a database so I want to make sure it isn't too large
> (ideally no more than 200K. I tried discovering the size of a JPEG
> using sizeof but that only works with built in types like int and long.
>
>
> regards,
>
> karen
>
>

 
Reply With Quote
 
Rad [Visual C# MVP]
Guest
Posts: n/a
 
      29th Dec 2006
On 29 Dec 2006 14:07:06 -0800, Karen Hill wrote:

> How do I get the file size of a JPEG and resize it if it is too big in
> terms of MB or KB? I am using Windows Forms, .NET 2.0, C#. I want to
> put this image in a database so I want to make sure it isn't too large
> (ideally no more than 200K. I tried discovering the size of a JPEG
> using sizeof but that only works with built in types like int and long.
>
> regards,
>
> karen


Hey Karen,

If its just the file size that you want you can do it using the FileInfo
object

FileInfo fi = new FileInfo(@"D:\Images\oldman.jpg");
Console.WriteLine(fi.Length);


--
Bits.Bytes
http://bytes.thinkersroom.com
 
Reply With Quote
 
Laurent Bugnion [ASP.NET MVP]
Guest
Posts: n/a
 
      1st Jan 2007
Hi,

Karen Hill wrote:
> How do I get the file size of a JPEG and resize it if it is too big in
> terms of MB or KB? I am using Windows Forms, .NET 2.0, C#. I want to
> put this image in a database so I want to make sure it isn't too large
> (ideally no more than 200K. I tried discovering the size of a JPEG
> using sizeof but that only works with built in types like int and long.
>
>
> regards,
>
> karen


The other replies show you how to get the file's size. To resize it, you
can use this (this example divides an image's size by 2 and saves it to
a given stream):

Bitmap originalImage = new Bitmap( path );
Bitmap newImage = new Bitmap( originalImage,
originalImage.Width / 2,
originalImage.Height / 2 );

newImage.Save( outputStream, originalImage.RawFormat );
originalImage.Dispose();
originalImage = null;

Note however that the built-in resizing algorithms are not very good,
and the resulting image might not look as nice as if you resized it
using a better algorithm, or an external program.

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Jpeg bloats access file size =?Utf-8?B?U2t5IFdhcnJlbg==?= Microsoft Access 11 18th Apr 2005 07:33 PM
decrease size of jpeg file =?Utf-8?B?SmVzdXM=?= Microsoft Dot NET Compact Framework 4 5th Jun 2004 02:01 PM
JPEG folder crash Explorer, corrupted JPEG, Nikon vs. Canon JPEG file format?? =?Utf-8?B?YXRsZXRpY28=?= Windows XP Photos 0 17th Feb 2004 03:51 PM
Need Program That Reduces The Size Of A JPEG File T. Lozengin Freeware 14 26th Nov 2003 11:55 AM
How do you reduce the size of a jpeg picture file G Windows XP Help 1 31st Oct 2003 04:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:17 PM.