当前位置: 首页>>代码示例>>C#>>正文


C# Bitmap.ToBytes方法代码示例

本文整理汇总了C#中System.Drawing.Bitmap.ToBytes方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.ToBytes方法的具体用法?C# Bitmap.ToBytes怎么用?C# Bitmap.ToBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Drawing.Bitmap的用法示例。


在下文中一共展示了Bitmap.ToBytes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ProcessImage

        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();
                }
            }
        }
开发者ID:kobyb1988,项目名称:PhotoBox,代码行数:27,代码来源:SimplePatternImageProcessor.cs

示例2: TestToBitmapFromExtractedBytes

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

                var reconstructedImage = bytes.ToBitmap();

                Assert.AreEqual(512, reconstructedImage.Width);
            }
        }
开发者ID:comsechq,项目名称:sugar,代码行数:11,代码来源:ByteArrayExtensionsTest.cs

示例3: GetCaptureForInstagramControl

        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();
                }
            }
        }
开发者ID:kobyb1988,项目名称:PhotoBox,代码行数:49,代码来源:ImageUtils.cs

示例4: TestToBytes

        public void TestToBytes()
        {
            using (var image = new Bitmap(ImageLocation))
            {
                var bytes = image.ToBytes(ImageFormat.Png);

                Assert.Less(0, bytes.Length);
            }
        }
开发者ID:comsechq,项目名称:sugar,代码行数:9,代码来源:BitmapExtensionsTest.cs

示例5: ProcessImages

        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();
            }
        }
开发者ID:kobyb1988,项目名称:PhotoBox,代码行数:58,代码来源:ImageUtils.cs


注:本文中的System.Drawing.Bitmap.ToBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。