通过写入字节在图片上写文本

时间:2013-10-13 13:42:30

标签: c# wpf image image-processing

我在下面的代码我有image2这是某个地方的图片和image3是白色平面中的文本(例如在默认的白色背景中用paint.exe写的“你好”)。

我想在图片上显示文字,但代码不成功。有什么问题?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;


namespace test_AlignmentOFImages
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        WriteableBitmap bitmap;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           byte []pixels=new byte[480*640*4];

            for (int i = 0; i < pixels.Length; i++)
            {
                if (i % 16 == 0)
                {
                    pixels[i] = 0xff;
                }
                else
                {
                    pixels[i] = (byte)i;
                }
                //pixels[i] = (byte)0xff;//white
            }
            int stride2 = 480 * 4;
            image2.Source = BitmapImage.Create((int)image2.Width, (int)image2.Height, 96, 96, PixelFormats.Bgra32, null, pixels, stride2);

            byte [] imagePixels=new byte[480*640*4];
            System.Drawing.Image img;
            //System.Drawing.Bitmap bm;
            try
            {
                //img = System.Drawing.Image.FromFile(@"E:\src\Tests\test_AlignmentOFImages\test_AlignmentOFImages\image3.jpg");
                img = System.Drawing.Image.FromFile(@"E:\src\Tests\test_AlignmentOFImages\test_AlignmentOFImages\image3.png");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            MemoryStream ms=new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); //img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

            Array.Copy(ms.ToArray(), imagePixels, ms.ToArray().Length);

            byte[] imagePixels2 = new byte[480 * 640*4];
            image3.Source = null;
            for (int i = 0; i < imagePixels.Length; i+=4)
            {
                if (imagePixels[i]<0xff  )//if it is not white
                {
                    imagePixels2[i] = imagePixels[i];//blue
                    imagePixels2[i+1] = imagePixels[i+1];//green
                    imagePixels2[i+2] = imagePixels[i+2];//red
                    imagePixels2[i+3] = 0xff;//alpha
                }


            }

            image3.Source = BitmapImage.Create((int)image3.Width, (int)image3.Height, 96, 96, PixelFormats.Bgra32, null, imagePixels2, stride2);
        }
    }
}

我认为我使用的是假像素格式,对于png或jpeg格式,我必须使用特殊的像素格式(例如bgr24或...)。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

MemoryStream ms=new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 

使用这些线条处理像素数据并将其转换为PNG文件。但是你想继续操纵像素数据。未格式化的PNG数据。

所以请改用LockBits()

imagePixels = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

下一个问题是你的复制方法。使用您提供的代码,您只需将image3.png复制到输出,丢弃任何Alpha通道而不考虑白色区域。不要分配新的像素数组。使用之前定义的pixels数组就足够了。

if (imagePixels[i]<0xff  )//if it is not white

此语句不检查像素是否为白色。它只是检查一个像素的红色通道是否为255.你也应该检查其他通道。

相关问题