Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
        private Stream ResizeImage(Stream image_stream, int width, int height, int quality, ImageType type)
        {
            try {
                // Load the image as a writeablebitmap
                WriteableBitmap writableBitmap;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(image_stream);
                writableBitmap = new WriteableBitmap(bitmapImage);

                double scale = Math.Min((double) width / writableBitmap.PixelWidth, (double) height / writableBitmap.PixelHeight);

                // No resize needed
                if (scale >= 1.0)
                    return image_stream;

                // Setup shorter names and pixelbuffers
                int w = writableBitmap.PixelWidth;
                int h = writableBitmap.PixelHeight;
                int[] p = writableBitmap.Pixels;
                byte[][,] imageRaster = new byte[3][,]; // RGB colors
                imageRaster[0] = new byte[w, h];
                imageRaster[1] = new byte[w, h];
                imageRaster[2] = new byte[w, h];

                // Copy WriteableBitmap data into buffer for FluxJpeg
                int i = 0;
                for (int y = 0; y < h; y++) {
                    for (int x = 0; x < w; x++) {
                        int color = p[i++];

                        imageRaster[0][x, y] = (byte) (color >> 16); // R
                        imageRaster[1][x, y] = (byte) (color >> 8);  // G
                        imageRaster[2][x, y] = (byte) (color);       // B
                    }
                }

                // Create new FluxJpeg image based on pixel data
                FluxJpeg.Core.Image jpegImage = new FluxJpeg.Core.Image(new ColorModel {
                    colorspace = ColorSpace.RGB
                }, imageRaster);

                // Calc new proportional size
                width = (int) Math.Round(writableBitmap.PixelWidth * scale);
                height = (int) Math.Round(writableBitmap.PixelHeight * scale);

                // Resize the image
                ImageResizer resizer = new ImageResizer(jpegImage);
                Image resizedImage = resizer.Resize(width, height, FluxJpeg.Core.Filtering.ResamplingFilters.LowpassAntiAlias);
                Stream imageStream = new MemoryStream();

                if (type == ImageType.Jpeg) {
                    // Encode the resized image as Jpeg
                    JpegEncoder jpegEncoder = new JpegEncoder(resizedImage, quality, imageStream);
                    jpegEncoder.Encode();
                } else {
                    int[] pixelBuffer = new int[resizedImage.Height * resizedImage.Width];
                    byte[][,] resizedRaster = resizedImage.Raster;

                    // Convert FJCore raster to PixelBuffer
                    for (int y = 0; y < resizedImage.Height; y++) {
                        for (int x = 0; x < resizedImage.Width; x++) {
                            int color = 0;

                            color = color | resizedRaster[0][x, y] << 16; // R
                            color = color | resizedRaster[1][x, y] << 8;  // G
                            color = color | resizedRaster[2][x, y];       // B

                            pixelBuffer[(y * resizedImage.Width) + x] = color;
                        }
                    }

                    // Encode the resized image as Png
                    PngEncoder pngEncoder = new PngEncoder(pixelBuffer, resizedImage.Width, resizedImage.Height, false, PngEncoder.FILTER_NONE, Deflater.BEST_COMPRESSION);
                    byte[] pngBuffer = pngEncoder.pngEncode();
                    imageStream.Write(pngBuffer, 0, pngBuffer.Length);
                }

                return imageStream;
            } catch {
                // Ignore the error and let the server resize the image
            }

            return image_stream;
        }
Ejemplo n.º 3
0
 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 "";
     }
 }