static Image Resize(string pathIn, int edge) { JpegDecoder decoder = new JpegDecoder(File.Open(pathIn, FileMode.Open)); DecodedJpeg jpeg = decoder.Decode(); ImageResizer resizer = new ImageResizer(jpeg.Image); return resizer.ResizeToScale(edge, ResamplingFilters.LowpassAntiAlias); }
public void Decode(DcmDataset dataset, DcmPixelData oldPixelData, DcmPixelData newPixelData, DcmCodecParameters parameters) { if (oldPixelData.NumberOfFrames == 0) return; // Determine JPEG image precision and assert that the implemented codec supports this precision int precision; try { precision = JpegHelper.ScanHeaderForBitDepth(oldPixelData); } catch (DicomCodecException) { precision = oldPixelData.BitsStored; } AssertImagePrecision(precision); // Ensure consistency in the new pixel data header if (precision > 8) newPixelData.BitsAllocated = 16; else if (newPixelData.BitsStored <= 8) newPixelData.BitsAllocated = 8; // Set up new pixel data specifics newPixelData.PhotometricInterpretation = newPixelData.PhotometricInterpretation.Equals("YBR_FULL_422") || newPixelData.PhotometricInterpretation.Equals("YBR_PARTIAL_422") ? "YBR_FULL" : oldPixelData.PhotometricInterpretation; if (newPixelData.PhotometricInterpretation.Equals("YBR_FULL")) newPixelData.PlanarConfiguration = 1; try { for (int j = 0; j < oldPixelData.NumberOfFrames; ++j) { var frameData = new byte[newPixelData.UncompressedFrameSize]; var jpegStream = new MemoryStream(oldPixelData.GetFrameDataU8(j)); // Decode JPEG from stream var decoder = new JpegDecoder(jpegStream); var jpegDecoded = decoder.Decode(); var img = jpegDecoded.Image; // Init Buffer int w = img.Width; int h = img.Height; var pixelsFromJpeg = img.Raster; // Copy FluxJpeg buffer into frame data array /* int comps = pixelsFromJpeg.GetLength(0); int preIncr = newPixelData.BytesAllocated - comps; if (preIncr < 0) throw new InvalidOperationException( String.Format("Number of JPEG components: {0} exceeds number of bytes allocated: {1}", comps, newPixelData.BytesAllocated)); */ int i = 0; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { var pixel = pixelsFromJpeg[0][x, y]; frameData[i++] = (byte)((pixel >> 8) & 0xff); frameData[i++] = (byte)(pixel & 0xff); // for (int k = 0; k < preIncr; ++k) frameData[i++] = 0xff; // for (int k = 0; k < comps; ++k) frameData[i++] = pixelsFromJpeg[k][x, y]; } } oldPixelData.Unload(); if (newPixelData.IsPlanar) DcmCodecHelper.ChangePlanarConfiguration(frameData, frameData.Length / newPixelData.BytesAllocated, newPixelData.BitsAllocated, newPixelData.SamplesPerPixel, 0); newPixelData.AddFrame(frameData); } } catch (Exception e) { Debug.Log.Error("Failed to decode JPEG image: {0}, reason: {1}", e.StackTrace, e.Message); } }
public string ImportarImagen(string NombreArchivo) { string MyPicturesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); string FilePath = MyPicturesPath + "\\" + NombreArchivo + ".jpg"; string TempPath = MyPicturesPath + "\\Temp.jpg"; try { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Imagenes |*.jpg;*.png;*.bmp"; if (dialog.ShowDialog() == true) { if (File.Exists(FilePath)) { File.Delete(FilePath); } File.Copy(dialog.File.FullName, FilePath); long fileLength = new FileInfo(FilePath).Length; //MessageBox.Show("tamaño anterior: " + fileLength); //Si la imagen escaneada es mayor 50Kb, se itera reduciendo la imagen a la mitad de su tamaño while (fileLength > 51200) { if (File.Exists(TempPath)) { File.Delete(TempPath); } if (File.Exists(FilePath)) { File.Copy(FilePath, TempPath); File.Delete(FilePath); } using (Stream Stream = File.OpenRead(TempPath)) { BitmapImage image = new BitmapImage(); image.SetSource(Stream); MemoryStream outStream = new MemoryStream(); Stream.Seek(0, SeekOrigin.Begin); JpegDecoder decoder = new JpegDecoder(Stream); DecodedJpeg jpeg = decoder.Decode(); ImageResizer resizer = new ImageResizer(jpeg.Image); FluxJpeg.Core.Image small = resizer.Resize(Convert.ToInt32(Math.Floor(image.PixelWidth / 2)), Convert.ToInt32(Math.Floor(image.PixelHeight / 2)), ResamplingFilters.NearestNeighbor); JpegEncoder encoder = new JpegEncoder(small, 90, outStream); encoder.Encode(); outStream.Seek(0, SeekOrigin.Begin); int bufferSize = Convert.ToInt32(outStream.Length); byte[] buffer = new byte[bufferSize]; outStream.Read(buffer, 0, bufferSize); outStream.Close(); Stream.Close(); File.WriteAllBytes(FilePath, buffer); } if (File.Exists(TempPath)) { File.Delete(TempPath); } fileLength = new FileInfo(FilePath).Length; } return FilePath; } else { return "Error: No se selecciono ningún archivo"; } } catch (Exception ex) { return ("Error de Aplicación: " + ex.Message); //return ""; } }