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

  • Thread starter Thread starter Karen Hill
  • Start date Start date
K

Karen Hill

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
 
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
}
 
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);
 
Hi,

Karen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top