For Programmers: Free Programming Magazines  


Home > Archive > C# > August 2007 > Image resize using GDI+ and Inadequate Memory Consumption









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Image resize using GDI+ and Inadequate Memory Consumption
fatr

2007-07-01, 5:08 am

I got to do some app which displays thumbinals of photos, my first idea was to change 'scalemode' of picturebox to 'zoom' only, but most of pictures with which my program will bedealing with got big resolution so loading about 20 files took huge amount of RAM.

Second thought was to resize images before putting them into picturebox and to recieve resized images i used folowing code:

static Image FixedSize(Image imgPhoto, int Width, int Height)
{

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);

//if we have to pad the height pad both the top and the bottom
//with the difference between the scaled height and the desired height
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((Width - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = (int)((Height - (sourceHeight * nPercent)) / 2);
}

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = InterpolationMode.Bilinear;
grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight),new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
grPhoto.Dispose();

return bmPhoto;
}
from this article
http://www.codeproject.com/csharp/imageresize.asp

I've expected in worst case consumption about 30MB of RAM 1MB for each 111x111 picture. But no matter how i try display images, both ways:
1. Each image in separate dynamically created picturebox control
2. Each image loaded one after another to the same single dinamically created pictureboxcontrol
Memory consumption is the same, about 110MB, below is the way in which i create this pictureboxes

private void button1_Click(object sender, EventArgs e)
{
PictureBox test;
int counter = 0,counter2 = 0;
int startx = 200, starty = 200, size = 111;
DirectoryInfo diSourceFolder = new DirectoryInfo("C:\\Pics\\");
test = new PictureBox(); //comment that line to get multiple pictureboxes
foreach (FileInfo fileName in diSourceFolder.GetFiles())
{
test = new PictureBox(); //comment that line to get single picturebox
test.Size = new Size(size, size);
test.Location = new Point(startx+size*counter+1,starty+size*
counter2+1);
test.Image = FixedSize(Image.FromFile(fileName.FullName), size, size);
this.Controls.Add(test);
counter++;
if (counter > 5) { counter2++; counter = 0;}
Application.DoEvents();
}
}

Does that behaviour is right? I'm worried because program will have to display about 80 or more photos at once, there is any way i could reduce this high consumption of RAM?
Adpoencehaup

2007-08-29, 2:37 am

Christina Applegate Shows Tiny Tits & Ass!

http://www.reyrewh.com/Play?movie=726648
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com