problem with thumbnail size

dinoboy

Member
Joined
Oct 11, 2003
Messages
24
Location
Estonia
Hi!
I generate thumbnails dynamically. The function has two parameters - filename and maxfilesize. And while thumbnail size in butes is bigger than the maxfilesize, it make new thumbnail again, only a smaller one. And my problem is how I can check the thumbnail size? It tried to save it to stream and then I took its length and compared it with maxfilesize and do the while loop. But when I tried to make a new bitmap with a new size, in the loop, then it told me The invalid parameter used

Are there any other solution or why it tells it?
 
Code:
public void OutputFile(string fileName, int maxThumbSize)
		{
			System.Drawing.Image g = System.Drawing.Image.FromFile(fileName);
			ImageFormat tFormat = g.RawFormat;
			
			Size tSize = this.genThumbSize((int) g.Width, (int) g.Height);
			Bitmap imgOutPut = new Bitmap(g, tSize.Width, tSize.Height);	
			
			MemoryStream imgStream = new MemoryStream();
			imgOutPut.Save(imgStream, tFormat);
			//imgOutPut.Dispose();
			
			while(imgStream.Length>maxThumbSize)
			{
				this.thumbSize = this.thumbSize-5;
				Size s = this.genThumbSize(imgOutPut.Width, imgOutPut.Height);
				imgOutPut = new Bitmap(System.Drawing.Image.FromFile(fileName), s.Width, s.Height);
				imgOutPut.Save(imgStream, tFormat);
			}
			
			//imgOutPut.Palette = 
			if(tFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
				Response.ContentType = "image/gif";
			else if(tFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
				Response.ContentType = "image/bmp";
			else if(tFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
				Response.ContentType = "image/jpeg";
			else if(tFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
				Response.ContentType = "image/png";
			
			imgOutPut.Save(this.Response.OutputStream, tFormat);
		}

This line gives me the Invalid parameter
imgOutPut = new Bitmap(System.Drawing.Image.FromFile(fileName), s.Width, s.Height);

Wihtout the while loop, its all ok, working just fine...
 
Try putting the Image.FromFile in its own Bitmap variable.
Bitmap b= (Bitmap)Image.FromFile(fileName);
imgOutPut = new Bitmap(b, s.Width, s.Height);
 
Last edited by a moderator:
I did that in while loop and before the while, but no use, it told me again Inavalid parameter used. :(

Hmm, maybe is there some problem with parameter nulling? or something :S
 
Last edited by a moderator:
I think it would give a null reference exception. But you can check each parameter before it is used in the method:
C#:
if(bitmap != null && imgOutPut != null)
{
  if(imgOutPut.Width > 0 && imgOutPut.Height > 0)
  {
    do code
  }
}
 
Hmm, it seems it dont help me. Because parameters have a values and not null. But still it is saying that.. :S
There are some problem with while loop but I dont know which :confused:
 
Thanks for the replys. But I already found a solution. I saved the temporary stream to a temp fail and then read it again and got the length and then deleted the file. And its working :)
 
Back
Top