Encoder for writing the data image to a stream in jpg format.
Наследование: IImageEncoder
Пример #1
0
        public static void SaveImage(this UIElement uiElement)
        {
            var dialog = new SaveFileDialog
            {
                DefaultExt = ".png",
                Filter = "PNG | *.png | JPG | *.jpg",
            };
            var save = dialog.ShowDialog();
            if (save.HasValue && save.Value)
            {
                var saveStream = dialog.OpenFile();

                var bitmap = new WriteableBitmap(uiElement, new TranslateTransform());
                var image = bitmap.ToImage();
                if (dialog.SafeFileName.EndsWith(".png"))
                {
                    var encoder = new PngEncoder();
                    encoder.Encode(image, saveStream);
                }
                else if (dialog.SafeFileName.EndsWith(".jpg"))
                {
                    var encoder = new JpegEncoder();
                    encoder.Encode(image, saveStream);
                }

                saveStream.Close();
            }
        }
        public static void SaveJpeg(this WriteableBitmap bitmap, Stream targetStream, int targetWidth, int targetHeight, int orientation, int quality)
        {
            ExtendedImage image = null;
            var disp = bitmap.Dispatcher;
            using (var are = new AutoResetEvent(false))
            {
                disp.BeginInvoke(() =>
                {
                    image = bitmap.ToImage();
                    are.Set();
                });
                are.WaitOne();
            }

            var resizer = new NearestNeighborResizer();
            var imageBaseOut = new ExtendedImage(targetWidth, targetHeight);
            resizer.Resize(image, imageBaseOut, targetWidth, targetHeight);
            var encoder = new JpegEncoder {Quality = quality};
            encoder.Encode(imageBaseOut, targetStream);
        }
Пример #3
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "JPEGファイル (*.jpg)|*.jpg|PNGファイル (*.png)|*.png|ビットマップファイル (*.bmp)|*.bmp";
            if (sfd.ShowDialog() == true)
            {
                IImageEncoder enc;
                string fn = sfd.SafeFileName.ToLower();
                if(fn.EndsWith(".jpg"))
                {
                    enc = new JpegEncoder();
                }
                else if (fn.EndsWith(".png"))
                {
                    enc = new PngEncoder();
                }
                else if (fn.EndsWith(".bmp"))
                {
                    enc = new BmpEncoder();
                }
                else
                {
                    MessageBox.Show("エラー");
                    return;
                }

                double scaledWidth = (cropImage.Width * zoom);
                double scaledHeight = (cropImage.Height * zoom);
                double scaledLeft = (imageWidth - scaledWidth) / 2;
                double scaledTop = (imageHeight - scaledHeight) / 2;
                Rect r = new Rect((double)cropCanvas.GetValue(Canvas.LeftProperty), (double)cropCanvas.GetValue(Canvas.TopProperty), cropCanvas.Width, cropCanvas.Height);
                Rect r2 = new Rect(scaledLeft, scaledTop, scaledWidth , scaledHeight);
                r.Intersect(r2);
                double clipedLeft = r.Left - scaledLeft;
                double clipedTop = r.Top - scaledTop;
                r.X = clipedLeft;
                r.Y = clipedTop;
                ScaleTransform st = new ScaleTransform();
                st.ScaleX = zoom;
                st.ScaleY = zoom;
                WriteableBitmap wb = new WriteableBitmap(cropImage, st);
                WriteableBitmap outputWb = new WriteableBitmap((int)r.Width, (int)r.Height);
                outputWb.Blit(new Rect(0, 0, r.Width, r.Height), wb, new Rect(r.Left, r.Top, r.Width, r.Height));
                ImageTools.Image img = ToImage(outputWb);
                using (Stream stream = sfd.OpenFile())
                {
                    enc.Encode(img, stream);
                }
            }
        }
Пример #4
0
        void saveImage(ImageTools.Image image, String filename, int num)
        {
            // get a path to save pictures
            String myPictures = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

            // create a new directory at that path
            DirectoryInfo lapseDir = Directory.CreateDirectory(myPictures + "\\" + filename + "\\" + camera.FriendlyName);

            // save the picture
            using (Stream stream = File.Create(lapseDir.FullName + "\\" + filename + num + ".jpg"))
            {

                // Declare jpeg encoder
                var encoder = new JpegEncoder();

                // Set the image quality
                encoder.Quality = 90;

                // Finally encode raw bitmap and save it as a jpg image
                encoder.Encode(image, stream);

                // Close the stream
                stream.Close();
            }
        }