public StreamHolder ResizeStream(Stream inputStream, string name, int maxWidth) { // Decode DecodedJpeg jpegIn = new JpegDecoder(inputStream).Decode(); if (!ImageResizer.ResizeNeeded(jpegIn.Image, maxWidth)) { return new StreamHolder(inputStream, name); } else { // Resize DecodedJpeg jpegOut = new DecodedJpeg( new ImageResizer(jpegIn.Image) .Resize(maxWidth, ResamplingFilters.NearestNeighbor), jpegIn.MetaHeaders); // Retain EXIF details // Encode var outputStream = new MemoryStream(); new JpegEncoder(jpegOut, 90, outputStream).Encode(); // Display outputStream.Seek(0, SeekOrigin.Begin); return new StreamHolder(outputStream, name); } }
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); }
private async void Button_Click(object sender, RoutedEventArgs e) { var openPicker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary }; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); var file = await openPicker.PickSingleFileAsync(); if (file == null) return; using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { // Display input image var imageIn = new BitmapImage(); await imageIn.SetSourceAsync(fileStream); InputImage.Source = imageIn; // Rewind fileStream.Seek(0); // Decode var jpegIn = new JpegDecoder(fileStream.AsStreamForRead()).Decode(); if (!ImageResizer.ResizeNeeded(jpegIn.Image, 320)) { OutputImage.Source = null; OutputText.Text = "No resize necessary."; return; } // Resize var jpegOut = new DecodedJpeg( new ImageResizer(jpegIn.Image) .ResizeToScale(320, ResamplingFilters.NearestNeighbor), jpegIn.MetaHeaders); // Retain EXIF details // Encode using (var outStream = new InMemoryRandomAccessStream()) { new JpegEncoder(jpegOut, 90, outStream.AsStreamForWrite()).Encode(); // Display outStream.Seek(0); var image = new BitmapImage(); await image.SetSourceAsync(outStream); OutputImage.Source = image; } } }
private void Button_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog() { Filter = "Image files (*.jpg)|*.jpg" }; if (ofd.ShowDialog() != true) return; Stream fileStream = ofd.File.OpenRead(); // Display input image Stream inStream = new MemoryStream(new BinaryReader(fileStream).ReadBytes((int)fileStream.Length)); BitmapImage imageIn = new BitmapImage(); imageIn.SetSource(inStream); InputImage.Source = imageIn; // Rewind fileStream.Seek(0, SeekOrigin.Begin); using (fileStream) { // Decode DecodedJpeg jpegIn = new JpegDecoder(fileStream).Decode(); if (!ImageResizer.ResizeNeeded(jpegIn.Image, 320)) { OutputImage.Source = null; OutputText.Text = "No resize necessary."; return; } // Resize jpegOut = new DecodedJpeg( new ImageResizer(jpegIn.Image) .ResizeToScale(320, ResamplingFilters.NearestNeighbor), jpegIn.MetaHeaders); // Retain EXIF details // Encode MemoryStream outStream = new MemoryStream(); new JpegEncoder(jpegOut, 90, outStream).Encode(); // Display outStream.Seek(0, SeekOrigin.Begin); BitmapImage image = new BitmapImage(); image.SetSource(outStream); OutputImage.Source = image; } }
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 ""; } }