SoloCodigo

CLR: .Net / Mono / Boo / Otros CLR => C# => Mensaje iniciado por: Painkiller en Sábado 10 de Abril de 2010, 00:24

Título: Problemas con metodo Save Imagenes.
Publicado por: Painkiller en Sábado 10 de Abril de 2010, 00:24
Que tal, pues he estado haciendo una aplicaci÷on que imprime la pantalla y la guarda como imagen png, el problema es que recientemente me empezo a dar problemas debido a que no guarda la extension especificada:
Código: C#
  1. public static byte[] takeScreenShot()
  2. {
  3.    // Set the bitmap object to the size of the screen
  4.    bmpScreenShot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
  5.    // Create a graphics object from the bitmap
  6.    gfxScreenShot = Graphics.FromImage(bmpScreenShot);
  7.   // Take the screenshot from the upper left corner to the right bottom corner
  8.   gfxScreenShot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
  9.   // Save the screenshot to the specified path that the user has chosen
  10.   bmpScreenShot.Save("Imagen" + index.ToString(), System.Drawing.Imaging.ImageFormat.Png);
  11.   byte[] size = getFileSize("Imagen" + index.ToString() + ".png") ;
  12.   byte[] name = System.Text.ASCIIEncoding.ASCII.GetBytes("|Imagen" + index.ToString() + ".png");
  13.   index++;
  14.   bmpScreenShot.Dispose();
  15.   gfxScreenShot.Dispose();
  16.   return joinByteArray(size, name);
  17. }
  18.  
Como dije la aplicación no arroja excepción alguna, pero al momento de mandar llamar a la funcion getFileSize arroja la excepción FileNotFoundException, algo que he notado es que esto siempre pasa en la tercera llamada a esta función, es decir el directorio se ve asi:
.
..
CapturaPantalla.exe
Imagen0.png
Imagen1.png
Imagen2
Imagen3

y asi sucesivamente, despues de ese punto, no importa cuantas veces se llame ya no agrega la extension, mas sin embargo los datos están ahi, ya que al agregarse la extension en el nombre se ve perfectamente. ¿Alguien sabe por que puede pasar esto?
Título: Re: Problemas con metodo Save Imagenes.
Publicado por: Jeysscarr en Sábado 10 de Abril de 2010, 02:06
Porque no pruebas con esto...
Lo hice con mi form... creo que para capturar toda la pantalla es aun mas sencillo si usas Screen.primaryScreen.Bounds

Obviamente deberas validar si ya existe un archivo llamado igual y todas esas cosas pero pienso que es mas sencillo este codigo

Código: C#
  1.  
  2.  
  3. //variable global
  4.  int cont = 0;
  5.  
  6.         void GuardarPantalla()
  7.         {
  8.             Graphics gr = this.CreateGraphics();
  9.             SizeF Tamaño = this.Size;
  10.             Bitmap bm = new Bitmap((int)Tamaño.Width, (int)Tamaño.Height);
  11.             Graphics gr2 = Graphics.FromImage(bm);
  12.             gr2.CopyFromScreen((int)this.Location.X, (int)this.Location.Y, 0, 0, Tamaño.ToSize());
  13.             bm.Save("Imagen"+ cont +".Png", System.Drawing.Imaging.ImageFormat.Png);
  14.             cont++;
  15.         }
  16.  
  17.         private void button2_Click(object sender, EventArgs e)
  18.         {
  19.             GuardarPantalla();
  20.         }
  21.  
  22.