使用WriteableBitmap修改JPG图像的像素

时间:2018-05-16 03:04:35

标签: c# writeablebitmap

我是C#和WPF编程的新手。我目前正在尝试弄清楚如何使用WriteableBitmap修改JPG图像。我想我得到了最新的一般要点,但我观察到一种我不明白的奇怪行为。

以下是基于以下示例代码的情况:如果我使用当前已注释掉的构造函数声明WriteableBitmap(第29行),我可以修改像素并使用其中的红色方块绘制一个黑色区域代码显示。这是我设置和编写像素的工作测试用例。

但是,如果我注释掉第29行并使用JPG图像创建一个WriteableBitmap(因为它当前编码 - 参见第31行),图像本身加载正常,但是我尝试覆盖的红色正方形和黑色正方形(以前工作过),不再出现。

任何人都可以解释为什么会这样吗?在第二种方法中,我没有声明dPiX和dPiY参数的值,也无法在任何地方指定BGRA32的PixelFormat。由于这些是只读的,我无法修改它们。这是我的问题的根源吗?如果是这样,有没有办法解决这个问题?

我觉得这是一个非常基本的东西,我只是在俯视。任何指导将不胜感激。提前感谢您的时间。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Drawing;
using System.IO;

namespace AsciiArt
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //Test code to load a JPG and then write over top of it using writeable bitmaps
        private void SampleWriteableBitmap()
        {
            // Create a writeable bitmap (which is a valid WPF image source
            //WriteableBitmap wbmap = new WriteableBitmap(100, 100, 96, 96, PixelFormats.Bgra32, null);

        WriteableBitmap wbmap = ConvertToBitmap("../../photos/Image1.JPG");

        //  Overwriting the image by making all pixels black -- a silly test I know.
        for (int x = 0; x < wbmap.PixelWidth; ++x)
        {
            for (int y = 0; y < wbmap.PixelHeight; ++y)
            {
                // apply pixels to bitmap
                //Colors are A, Red, Green, Blue.  A must be 255 to be opaque
                //                                                  A    R  G  B
                SetPixel(wbmap, x, y, System.Drawing.Color.FromArgb(255, 0, 0, 0));
            }
        }

        // and then creating a smaller block of a different color -- again as  a test.
        for (int x = 0; x < 20; ++x)
        {
            for (int y = 0; y < 20; ++y)
            {
                // apply pixels to bitmap
                //Colors are A, Red, Green, Blue.  A must be 255 to be opaque
                //                                                   A    R  G  B
                SetPixel(wbmap, x, y, System.Drawing.Color.FromArgb(255, 255, 0, 0));
            }
        }

        // Display some parameters for debugging...
        ByteReadData.Text = "wbmap.PixelHeight: " + wbmap.PixelHeight + "    wbmap.PixelWidth: " + 
            wbmap.PixelWidth + "   wbmap.Format: " + wbmap.Format.BitsPerPixel + " ---> " + 
            wbmap.PixelHeight * wbmap.PixelWidth * wbmap.Format.BitsPerPixel / 8 + "   wbmap.dpix: " + wbmap.DpiX + "   wbmap.dpiy: " + wbmap.DpiY;

        // set image source to the new bitmap
        convertedPhoto.Source = wbmap;
    }

    // function to convert a JPG or other file type to a Bitmap
    public WriteableBitmap ConvertToBitmap(string fileName)
    {
        BitmapImage bitmap = new BitmapImage(new Uri(fileName, UriKind.Relative));
        WriteableBitmap newbitmap = new WriteableBitmap(bitmap);

        return newbitmap;
    }

    public void SetPixel(WriteableBitmap wbm, int x, int y, System.Drawing.Color c)
    {
        if (y > wbm.PixelHeight - 1 || x > wbm.PixelWidth - 1) return;
        if (y < 0 || x < 0) return;
        if (!wbm.Format.Equals(PixelFormats.Bgra32)) return;
        wbm.Lock();
        IntPtr buff = wbm.BackBuffer;
        int Stride = wbm.BackBufferStride;
        unsafe
        {
            byte* pbuff = (byte*)buff.ToPointer();
            int loc = y * Stride + x * 4;
            pbuff[loc] = c.B;
            pbuff[loc + 1] = c.G;
            pbuff[loc + 2] = c.R;
            pbuff[loc + 3] = c.A;
        }
        wbm.AddDirtyRect(new Int32Rect(x, y, 1, 1));
        wbm.Unlock();
    }

    public MainWindow()
    {
        InitializeComponent();

        SampleWriteableBitmap();
    }
}

} }

0 个答案:

没有答案