Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting size (ko) of an image in memory

Status
Not open for further replies.

ZeeNer

Programmer
Jun 2, 2002
38
0
0
CA
Hello there,

Im doing a windows application that basicaly read a .xls wich contains a lot of FILENAME, those filenames are all JPG files and i need to resize them to create 2 sizes of thumbnails (120x? and 260x?). This is done and work well.

But i also need to estimate the size that the file will have on disk when downloaded on 320x? , 800x?, 1024x? and 1080x?) , my application need this information and will store this when i'll write everything in a MYSQL database. The idea is that i dont want those pictures on disk, i'll resize them on demand and send them to the client with a .ASPX page.

Here is a sample of what i do for estamating the size of the 320, :

//imgBase contains information about the image, with Drawing.Image property.

double size;
ImageDimensions imgDim = null;
MemoryStream stream = new MemoryStream()
imgDim = ImageManager.ResizeImageInMemory(imgBase, stream, 320);
size = (stream.ToArray().Length * 1.0) / 1024;

Here's the problem, the estimation with this is WRONG, it gives me for exemple 23ko for a 320x? image, but when i download it its 42ko... almost 2x my estimation. :(

Anyone can tell me if there's a way to do what i need ? It seems like saving a picture in a memorystream doesnt keep every information to have a true estimation WITHOUT writing any image to disk.

Sorry for my poor english, i did my best ;)

 
My code use many classes so its hard for me to put every information you could want...

For the resize you may want to know what my Manager is doing... Here are the 2 main functions you may want to look at.


/// <param name="xSize">Largeur</param>
/// <param name="ySize">Hauteur</param>
/// <param name="stream">stream en mémoire</param>
/// <returns>image redimentionnée</returns>
public ImageDimensions ResizeToJpeg(int xSize, int ySize, Stream stream)
{
this.RGBRezise(xSize, ySize);
this.Save(stream, ImageFormat.Jpeg);

ImageDimensions imgDim = new ImageDimensions(scaledBitmap.PhysicalDimension.Width, scaledBitmap.PhysicalDimension.Height);

scaledBitmap.Dispose();

return imgDim;
}


/// <param name="fileName">fichier à créer</param>
/// <param name="format">format demandé</param>
void Save(string fileName, ImageFormat format)
{
scaledBitmap.Save(fileName, format);
}

Thankx for the help
 
Hum sorry i didnt paste the right "Save" override for my exemple

void Save(Stream stream, ImageFormat format)
{
scaledBitmap.Save(stream, format);
}

scaledBitmap is a private member in my ThumbManager class that i use to resize images.
 
With some tests i found that the size i was getting from my stream was actually good. So the problem was in fact in my .ASPX , it seems that when you send a file from a stream or a byte of array and you DONT specify the size you want to send , it's sending a file larger than the one in the stream...

He's what i did

string docPath = ViewState[KeyParamDocumentPath].ToString();

ImageInfo imgInfo = ImageManager.GetImageInfo(docPath);

MemoryStream imageStream = new MemoryStream();

Resize(imgInfo, imageStream, sizeWanted);

byte[] imageContent = new Byte[imageStream.Length];

// rewind the memory stream
imageStream.Position = 0;

// load the byte array with the image
imageStream.Read(imageContent, 0, (int)imageStream.Length);

SetMode(imgInfo.GetImageFormat(), imgInfo.Nom, (int)imageStream.Length);

Response.BinaryWrite(imageContent);


----

private void SetMode(ImageFormat format, string nomImage, int size)
{
Response.ContentType = "application/octet-stream";
if(size > 0)
Response.AppendHeader("Content-Length", size.ToString());
Response.AppendHeader("Content-Disposition", "attachment; " + "inline;filename=\"" + nomImage + "\";");
}


Response.AppendHeader("Content-Length", size.ToString());

 
Response.AppendHeader("Content-Length", size.ToString());


This is what i was missing, and it makes the image send to the client at the right size.
 
It seems like saving a picture in a memorystream doesnt keep every information to have a true estimation WITHOUT writing any image to disk.

Are you counting in the fact that on different filesystems (NTFS, FAT32) there are different blocksizes? For example, NTFS's default blocksize is 4kb, so if you store a file that is 1kb, it will always take up 4kb, as that's the next blocksize higher.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top