How to capture file size from System.Drawing.Image?

Asked

Viewed 968 times

1

I have a method to which I pass the parameter System.Drawing.Image and the percentage that this image will get after the resize.

public static Image ResizeImagem(Image imgFoto, int percentual)
{
  float nPorcentagem = ((float)percentual / 100);

  int fonteLargura = imgFoto.Width;     //armazena a largura original da imagem origem
  int fonteAltura = imgFoto.Height;   //armazena a altura original da imagem origem
  int origemX = 0;        //eixo x da imagem origem
  int origemY = 0;        //eixo y da imagem origem

  int destX = 0;          //eixo x da imagem destino
  int destY = 0;          //eixo y da imagem destino
  //Calcula a altura e largura da imagem redimensionada
  int destWidth = (int)(fonteLargura * nPorcentagem);
  int destHeight = (int)(fonteAltura * nPorcentagem);

  //Cria um novo objeto bitmap
  Bitmap bmImagem = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
  //Define a resolu~ção do bitmap.
  bmImagem.SetResolution(imgFoto.HorizontalResolution, imgFoto.VerticalResolution);
  //Crima um objeto graphics e defina a qualidade
  Graphics grImagem = Graphics.FromImage(bmImagem);
  grImagem.InterpolationMode = InterpolationMode.HighQualityBicubic;

  //Desenha a imge usando o método DrawImage() da classe grafica
  grImagem.DrawImage(imgFoto,
      new Rectangle(destX, destY, destWidth, destHeight),
      new Rectangle(origemX, origemY, fonteLargura, fonteAltura),
      GraphicsUnit.Pixel);

  grImagem.Dispose();  //libera o objeto grafico
  return bmImagem;
}

But I need to get the size of this file, in bytes for me to perform the Resize until the image is 200KB.

  • You need the size of any file, right? Has any special reason for this to be directly related to System.Drawing.Image?

  • @bigown, I have a method that gets Image, I will post

  • @bigown I edited the question, I think it became clearer

  • I don’t think so. It either didn’t help or confirmed what I was saying, no matter what’s in this file, you want its size and period. It would be clearer if you show that being an image makes a difference. I’ll answer what I think solves the problem. If it’s not that, you talk and I try to get better.

1 answer

3


From what I understand what you need is to know the size of any file. It makes no difference what is inside it. The result will be no different if you have a text, an image, a database structure, a song or anything else.

For this you use the class FileInfo and takes the Length. basically that’s it:

long tamanho = new System.IO.FileInfo(path).Length; //path é o caminho completo do arquivo.

But if you need to take this information still in memory you can do something like this:

long tamanho;
using (var ms = new System.IO.MemoryStream()) {
    imgFoto.Save(ms, ImageFormat.Jpeg);
    tamanho = ms.Length;
}

I put in the Github for future reference.

Choose the format you want to "save" the image. Choose ImageFormat. Note that this is not saving to disk file. I cannot guarantee that after saving to disk it will be exactly the same size. There may be some small variation, very small indeed.

  • ,the code really picked up the size I wanted, however while making a While, and go rendenizing her, I think she’s saving on the same Memorysteam, and adding up values, how could I close the Memorystream and open it again ?

  • It depends on the way you’re doing it. If you use exactly this, it’s closing every time. http://msdn.microsoft.com/en-us/library/yh598w02.aspx. You probably have your code inside the using, there will be trouble. This code is to be used alone. The only thing it relates to the rest of your code is the use of imgFoto which must already exist. If something is still wrong, the problem is in this object that is being reused rather than generating a new one. Then the problem would be something else, nothing related to your question.

  • Got it @bigown, well, so knowing where the bug is, I’ll see you here, thanks

  • It is possible (although I think not) that the problem is in this grImagem.Dispose();. Actually I think it might be related. This is a wrong practice. If any problem happens the code will never be called. It is likely that you have used this pattern elsewhere and are causing problems. The Dispose() It should never be called in code unless you have a very strong reason for it. Your call must be guaranteed by the language, so I used the using, he guarantees it. The bmImagem nor the Dispose() makes. You’re leaving garbage all over your memory.

  • I tried to set it as Null, but I’ll try it here, thank you

  • only one feedback, the error was in Memorystream, whenever making Image.Save(ms,Imageformart.jpeg) it saved in memoryStream, so I set it as null and instantiated again to have a new memory space

  • this is wrong, there is almost never a reason to set null for something existing. You may have even fixed the problem in brute force but there is error there. As I am not seeing your code I can not give details but you used somehow that is reusing the object and this is the error. The way I posted it I shouldn’t be doing this.

  • my code is as follows: http://pastebin.com/s77N5btP if you would like to make an opinion

  • You didn’t use the MemoryStream the way I gave it to you. You took the lock off of it. You let the memory leak. To tell you the truth this code is causing a chaos in memory.

  • See that in While, I do the following, I convert the Image and add it to Memorystream, until it gets the size past KB, using Using, he kept adding the Image every time in Memorystream, causing his size to always increase and not decrease as I wanted... I don’t know how else to do it

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.