private byte[] ProcessImage(byte[] image)
        {
            using (MemoryStream backgroundStream = new MemoryStream(Pattern.Data))
            {
                Image backgroundImage = Image.FromStream(backgroundStream);
                using (Bitmap backgroundBitmap = new Bitmap(backgroundImage.Width, backgroundImage.Height))
                {
                    using (Graphics canvas = Graphics.FromImage(backgroundBitmap))
                    {
                        canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        canvas.DrawImage(backgroundImage, new Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height),
                            new Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height), GraphicsUnit.Pixel);

                        int destWidth = backgroundBitmap.Width * 3 / 4;
                        int destHeight = backgroundBitmap.Height * 3 / 4;
                        int stepX = backgroundBitmap.Width / 6;
                        int stepY = backgroundBitmap.Height / 6;

                        FillCanvas(canvas, image, new Point(0, 0), destWidth, destHeight, 0, 0); //todo offset

                        canvas.Save();
                    }

                    return backgroundBitmap.ToBytes();
                }
            }
        }
        public void TestToBitmapFromExtractedBytes()
        {
            using (var image = new Bitmap(ImageLocation))
            {
                var bytes = image.ToBytes(ImageFormat.Png);

                var reconstructedImage = bytes.ToBitmap();

                Assert.AreEqual(512, reconstructedImage.Width);
            }
        }
Beispiel #3
0
        public byte[] GetCaptureForInstagramControl(byte[] imageForInstagram, string publishAuthorName, DateTime timeUpdate, byte[] profilePictureData)
        {
            byte[] captureImageData;
            var defaultTemplate = new InstagramDefaultCtrl();
            defaultTemplate.FillData(imageForInstagram,publishAuthorName,timeUpdate,profilePictureData);
            defaultTemplate.Measure(new System.Windows.Size((int)defaultTemplate.Width, (int)defaultTemplate.Height));
            defaultTemplate.Arrange(new Rect(new System.Windows.Size((int)defaultTemplate.Width, (int)defaultTemplate.Height)));

            int dpiX = 96;
            int dpiY = 96;
            Rect bounds = VisualTreeHelper.GetDescendantBounds(defaultTemplate);
            RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)(bounds.Width * dpiX / 96.0),
                                           (int)(bounds.Height * dpiY / 96.0), dpiX, dpiY, PixelFormats.Pbgra32);

            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(defaultTemplate);
                ctx.DrawRectangle(vb, null, new Rect(new System.Windows.Point(), bounds.Size));
            }

            renderTarget.Render(dv);

            JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
            jpgEncoder.QualityLevel = 100;
            jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
            using (MemoryStream outputStream = new MemoryStream())
            {
                jpgEncoder.Save(outputStream);
                captureImageData = outputStream.ToArray();
            }
            using (Bitmap rectangleResultImage = new Bitmap((int)bounds.Width, (int)bounds.Height))
            {
                using (Graphics canvas = Graphics.FromImage(rectangleResultImage))
                {
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    using (var imageStream = new MemoryStream(captureImageData))
                    {
                        System.Drawing.Image squereResultImage = System.Drawing.Image.FromStream(imageStream);
                        canvas.DrawImage(squereResultImage,
                        new Rectangle(0, 0, squereResultImage.Width, squereResultImage.Height),
                        new Rectangle(0, 0, squereResultImage.Width, squereResultImage.Height), GraphicsUnit.Pixel);
                    }

                    return rectangleResultImage.ToBytes();
                }
            }
        }
Beispiel #4
0
        public void TestToBytes()
        {
            using (var image = new Bitmap(ImageLocation))
            {
                var bytes = image.ToBytes(ImageFormat.Png);

                Assert.Less(0, bytes.Length);
            }
        }
Beispiel #5
0
        public byte[] ProcessImages(List<byte[]> images, Size liveViewImageStreamSize, Template pattern)
        {
            var defWidth = liveViewImageStreamSize.Width;
            var defHeight = liveViewImageStreamSize.Height;

            var hasBackground = pattern.Background != null;
            var hasOverlay = pattern.Overlay != null;

            var backgroundStream = hasBackground ? new MemoryStream(pattern.Background.Data) : null;
            var overlayStream = hasOverlay ? new MemoryStream(pattern.Overlay.Data) : null;
            var backgroundImage = backgroundStream.Return(Image.FromStream, null);
            var overlayImage = overlayStream.Return(Image.FromStream, null);

            var width = Math.Max(backgroundImage.Return(x => x.Width, 0), overlayImage.Return(x => x.Width, 0));
            var height = Math.Max(backgroundImage.Return(x => x.Height, 0), overlayImage.Return(x => x.Height, 0));

            if (width == 0 && height == 0)
            {
                width = defWidth;
                height = defHeight;
            }

            using (var backgroundBitmap = new Bitmap(width, height))
            {
                using (var canvas = Graphics.FromImage(backgroundBitmap))
                {
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    // ReSharper disable once AccessToDisposedClosure
                    backgroundImage.Do(x => canvas.DrawImage(x,
                        new Rectangle(0, 0, width, height),
                        new Rectangle(0, 0, x.Width, x.Height), GraphicsUnit.Pixel));

                    var templates = pattern.Images.ToList();

                    for (var i = 0; i < images.Count; i++)
                    {
                        var template = templates[i];

                        var destWidth = (int)Math.Round(width * template.Width);
                        var destHeight = (int)Math.Round(height * template.Height);
                        var stepX = (int)Math.Round(width * template.X);
                        var stepY = (int)Math.Round(height * template.Y);

                        FillCanvas(canvas, images[i], new Point(stepX, stepY), destWidth, destHeight, 0, 0);
                    }

                    // ReSharper disable once AccessToDisposedClosure
                    overlayImage.Do(x => canvas.DrawImage(x,
                        new Rectangle(0, 0, width, height),
                        new Rectangle(0, 0, x.Width, x.Height), GraphicsUnit.Pixel));

                    canvas.Save();
                }

                return backgroundBitmap.ToBytes();
            }
        }