static ImageU() { Empty = new ImageU(); Empty.Name = String.Empty; Empty.Format = Format.NAN; Empty.Value = unchecked ((int)double.NaN); }
private void ImagenGifFromFile(int index) { Debug.WriteLine("ImageBox_ImagenGifFromFile()"); ImageU imgU = ImagenList[index]; try { Dictionary <Guid, ImageFormat> guidToImageFormatMap = new Dictionary <Guid, ImageFormat>() { { ImageFormat.Bmp.Guid, ImageFormat.Bmp }, { ImageFormat.Gif.Guid, ImageFormat.Png }, { ImageFormat.Icon.Guid, ImageFormat.Png }, { ImageFormat.Jpeg.Guid, ImageFormat.Jpeg }, { ImageFormat.Png.Guid, ImageFormat.Png } }; using (Image img = Image.FromFile(imgU.Name, true)) { //Check the image format to determine what //format the image will be saved to the //memory stream in ImageFormat imageFormat = null; Guid imageGuid = img.RawFormat.Guid; foreach (KeyValuePair <Guid, ImageFormat> pair in guidToImageFormatMap) { if (imageGuid == pair.Key) { imageFormat = pair.Value; break; } } if (imageFormat == null) { throw new NoNullAllowedException("Unable to determine image format"); } //Get the frame count var dimension = new FrameDimension(img.FrameDimensionsList[0]); //int iCount = img.GetFrameCount(Dimension); img.SelectActiveFrame(dimension, imgU.Value); using (MemoryStream ms = new MemoryStream()) { img.Save(ms, imageFormat); Imagen = Image.FromStream(ms); } } } catch (Exception ex) { Debug.WriteLine( "Error type: " + ex.GetType().ToString() + "\nMessage: " + ex.Message + "\nError in " + MethodBase.GetCurrentMethod().Name + "\n" ); } }
public void FromFile(string pathImage) { //se supone que existe. Debug.WriteLine("ImageBox_FromFile()"); if (!File.Exists(pathImage)) { return; } using (var stream = File.Open(pathImage, FileMode.Open)) { Image img = Image.FromStream(stream); //ImageFormat formato = GetImagenFormat(img); //string ext = Path.GetExtension(pathImage).ToUpper(); //if (ext == ".GIF") { //cargar valores del gif var dimension = new FrameDimension(img.FrameDimensionsList[0]); int iCount = img.GetFrameCount(dimension); for (int i = 0; i < iCount; i++) { ImageU imgU = new ImageU(pathImage, Format.GIF, i); ImagenList.Add(imgU); } _count = ImagenList.Count; img.Dispose(); } // /*if (ext == ".JPG") { * ImageU imgU = new ImageU(pathImage, Format.JPG, 0); * ImagenList.Add(imgU); * _count = ImagenList.Count; * }*/ //img.Dispose(); if (Count <= 0) //no hay imagen que mostrar { CurrentFrame = -1; } else { GetNextFrame();//se muestra la primera imagen cargada } this.Invalidate(this.ClientRectangle); //todo: se extrae la conclusion que solo necesitmaos //el nombre del fichero y la frameCount de la imagen //que pretendemos extraer. }
private void ImagenFromFile(int index) { //se supone que el indice está dentro de los parametros Debug.WriteLine("ImageBox_ImagenFromFile()"); if (ImagenList == null || Count == 0) { return; } ImageU imgU = ImagenList[index]; if (imgU.Format == Format.GIF) { ImagenGifFromFile(index); } if (imgU.Format == Format.JPG) { Imagen = Image.FromFile(imgU.Name, true); } this.Invalidate(this.ClientRectangle); }
private void TasKSaveImageGif(string pathfile) { if (ImagenList.Count == 0 || ImagenList == null) { return; } Debug.WriteLine("Iniciando ... taskSaveImageGif()"); try { ImageU imgu; List <ImageU> ImagenRedim = new List <ImageU>(); string dir = Path.GetTempPath(); string nametemp = Path.GetTempFileName(); int wd = Imagen.Width; int hd = Imagen.Height; for (int i = 0; i < ImagenList.Count; i++) { nametemp = Path.Combine(dir, Path.GetTempFileName()); imgu = ImagenList[i]; if (imgu.Format == Format.GIF) { using (Image img = Image.FromFile(imgu.Name, true)) { var dimension = new FrameDimension(img.FrameDimensionsList[0]); img.SelectActiveFrame(dimension, imgu.Value); using (MemoryStream ms = new MemoryStream()) { img.Save(ms, ImageFormat.Jpeg); Image imgp = Image.FromStream(ms); Image imgpaso = Utility.ResizeImage(imgp, wd, hd, true); imgpaso.Save(nametemp + ".jpg", ImageFormat.Jpeg); ImageU paso = new ImageU(nametemp + ".jpg", Format.GIF, 0); ImagenRedim.Add(paso); imgpaso.Dispose(); imgp.Dispose(); } } } if (imgu.Format == Format.JPG) { using (Image img = Image.FromFile(imgu.Name, true)) { Image imgpaso = Utility.ResizeImage(img, wd, hd, true); imgpaso.Save(nametemp + ".jpg", ImageFormat.Jpeg); ImageU paso = new ImageU(nametemp + ".jpg", Format.JPG, 0); ImagenRedim.Add(paso); imgpaso.Dispose(); } } } AnimatedGifEncoder egif = new AnimatedGifEncoder(); egif.Start(pathfile); egif.SetDelay(Time); egif.SetRepeat(0); for (int i = 0; i < ImagenRedim.Count; i++) { using (var stream = File.Open(ImagenRedim[i].Name, FileMode.Open)) { egif.AddFrame(Image.FromStream(stream)); } //añadirla. } egif.Finish(); for (int i = 0; i < ImagenRedim.Count; i++) { File.Delete(ImagenRedim[i].Name); } } catch (Exception ex) { Debug.WriteLine( "Error type: " + ex.GetType().ToString() + "\nMessage: " + ex.Message + "\nError in " + MethodBase.GetCurrentMethod().Name + "\n" ); } Debug.WriteLine("Finalizada la construccion del Gif: " + pathfile); }