如何将文本添加到一个位图图像编程? WPF [英] How to add text to a bitmap image programmatically? WPF

查看:145
本文介绍了如何将文本添加到一个位图图像编程? WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Kinect感应器通过设置视频输入作为位图源如下图所示,显示图像的视频饲料。但我的问题是如何将我的文字添加到图像/位图例如比分反超,我添加了以下图片显示我想要达到的。

I'm using a Kinect sensor to show a video feed on an image by setting the video feed as bitmap source like shown below. But my question is how would I add text to the image/bitmap for example a score counter, I added a picture below to show what I'm trying to achieve.

void myKinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {

                if (colorFrame == null) return;
                byte[] colorData = new byte[colorFrame.PixelDataLength];
                colorFrame.CopyPixelDataTo(colorData);

                 KinectVideo.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96,
                    PixelFormats.Bgr32, null, colorData, colorFrame.Width * colorFrame.BytesPerPixel);

            }
        } 

推荐答案

您可以通过实现这个 DrawingVisual DrawingImage 类:

You can achieve this using DrawingVisual and DrawingImage classes :

var random = new Random();
var pixels = new byte[256 * 256 * 4];
random.NextBytes(pixels);
BitmapSource bitmapSource = BitmapSource.Create(256, 256, 96, 96, PixelFormats.Pbgra32, null, pixels, 256 * 4);
var visual = new DrawingVisual();
using (DrawingContext drawingContext = visual.RenderOpen())
{
    drawingContext.DrawImage(bitmapSource, new Rect(0, 0, 256, 256));
    drawingContext.DrawText(
        new FormattedText("Hi!", CultureInfo.InvariantCulture, FlowDirection.LeftToRight,
            new Typeface("Segoe UI"), 32, Brushes.Black), new Point(0, 0));
}
var image = new DrawingImage(visual.Drawing);
Image1.Source = image;

不幸的是,你必须创建一个新的的BitmapSource ,因为目前还没有办法,我知道直接写入文字吧。

Unfortunately you will have to create a new BitmapSource as there's currently no way I know of writing text directly to it.

另外,您可以使用 WriteableBitmapEx HTTPS://writeablebitmapex.$c $ cplex.com /


  • 从你的框架使用创建WriteableBitmap的 BitmapFactory (1)

  • 创建另一个WriteableBitmap的,并使用上面的方法就可以绘制文本(2)

  • blit的文本位图(2)在你的框架(1)

同样的结果,但不同的方法,不能确定是否方法2是更好,因为它的繁琐。

Same result but different approach, not sure whether approach 2 is better as it's cumbersome.

这篇关于如何将文本添加到一个位图图像编程? WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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