示例#1
0
        public static ImageResult CreateImageFrom(byte[] bytes, ImageFormat format)
        {
            byte[] nonIndexedImageBytes;

            using (var memoryStream = new MemoryStream(bytes))
                using (var image = Image.FromStream(memoryStream))
                    using (var temporaryBitmap = new Bitmap(image.Width, image.Height))
                    {
                        using (var graphics = Graphics.FromImage(temporaryBitmap))
                        {
                            graphics.DrawImage(image, new Rectangle(0, 0, temporaryBitmap.Width, temporaryBitmap.Height),
                                               0, 0, temporaryBitmap.Width, temporaryBitmap.Height, GraphicsUnit.Pixel);
                        }

                        ImagePropertyItems.Copy(image, temporaryBitmap);

                        using (var saveStream = new MemoryStream())
                        {
                            temporaryBitmap.Save(saveStream, format);
                            nonIndexedImageBytes = saveStream.ToArray();
                        }
                    }

            return(new ImageResult(nonIndexedImageBytes, format));
        }
        public Image Crop()
        {
            CalculateCropSizes();
            CreateEmptyBitmap();
            DrawCroppedImageOnBitmap();

            ImagePropertyItems.Copy(_image, _croppedImage);

            return(_croppedImage);
        }
示例#3
0
        protected static Image ResizeImageToViewport(Image image, int targetWidth, int targetHeight)
        {
            var resizedImage = new Bitmap(targetWidth, targetHeight);

            resizedImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            using (var graphics = Graphics.FromImage(resizedImage))
            {
                SetHighestQualitySettings(graphics);
                graphics.DrawImage(image, 0, 0, targetWidth, targetHeight);
            }

            ImagePropertyItems.Copy(image, resizedImage);

            return(resizedImage);
        }