Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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, it's all ok, working just fine...

Posted (edited)

Try putting the Image.FromFile in it's own Bitmap variable.

Bitmap b= (Bitmap)Image.FromFile(fileName);

imgOutPut = new Bitmap(b, s.Width, s.Height);

Edited by aewarnick
C#
Posted (edited)

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

Edited by dinoboy
Posted

I think it would give a null reference exception. But you can check each parameter before it is used in the method:

if(bitmap != null && imgOutPut != null)
{
 if(imgOutPut.Width > 0 && imgOutPut.Height > 0)
 {
   do code
 }
}

C#
Posted

Hmm, it seems it don't 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 don't know which :confused:

Posted
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 it's working :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...