在 WPF 中在运行时创建 tiff 图像 [英] Creating tiff image at runtime in WPF

查看:34
本文介绍了在 WPF 中在运行时创建 tiff 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行时生成一个简单的 tiff 图像.此图片由白色背景和从远程服务器下载的图片组成.

I'm trying to generate a simple tiff image at runtime. This image consists of white background and image downloaded from remote server.

这是我为实现该目标而编写的代码:

Here's the code I've written for reaching that goal:

        const string url = "http://localhost/barcode.gif";

        var size = new Size(794, 1123);
        var drawingVisual = new DrawingVisual();

        using (var drawingContext = drawingVisual.RenderOpen())
        {
            drawingContext.DrawRectangle(new SolidColorBrush(Colors.White), null, new Rect(size));

            var image = new BitmapImage(new Uri(url));
            drawingContext.DrawImage(image, new Rect(0, 0, 180, 120));
        }

        var targetBitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Default);
        targetBitmap.Render(drawingVisual);

        var convertedBitmap = new FormatConvertedBitmap(targetBitmap, PixelFormats.BlackWhite, null, 0);

        var encoder = new TiffBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(convertedBitmap));

        using (var fs = new FileStream("out.tif", FileMode.Create))
        {
            encoder.Save(fs);
        }

代码工作并生成out.tif"文件.但是输出文件只有白色背景,没有从远程服务器接收到的图像.

The code works and generates "out.tif" file. But the output file has just a white background, without image received from remote server.

可能是什么问题?我以多种方式尝试了以下代码,但每次都没有运气.

What can be the problem? I have tried the following code in a variety of ways, but everytime with no luck.

推荐答案

我找到了这篇关于 FormatConvertedBitmap 类的文章.也许试一试

I found this reading about the FormatConvertedBitmap class. Maybe give it a shot

        FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();

        // BitmapSource objects like FormatConvertedBitmap can only have their properties
        // changed within a BeginInit/EndInit block.
        newFormatedBitmapSource.BeginInit();

        // Use the BitmapSource object defined above as the source for this new 
        // BitmapSource (chain the BitmapSource objects together).
        newFormatedBitmapSource.Source = targetBitmap;

        // Set the new format to BlackWhite.
        newFormatedBitmapSource.DestinationFormat = PixelFormats.BlackWhite;
        newFormatedBitmapSource.EndInit();

这篇关于在 WPF 中在运行时创建 tiff 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆