Wpf 为什么这个图像转换器没有';不行?

Wpf 为什么这个图像转换器没有';不行?,wpf,image,Wpf,Image,我只是在WPF中玩图像,我编写了一个示例应用程序,从硬盘加载一个图像,并且应该将每行的前100个像素更改为红色。但它并没有做到这一点,而是生成的iage到处都有小红线。我相信在代码的某个地方,h和w发生了变化,因此步幅是不正确的。但我找不到问题出在哪里。谢谢你的帮助 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.T

我只是在WPF中玩图像,我编写了一个示例应用程序,从硬盘加载一个图像,并且应该将每行的前100个像素更改为红色。但它并没有做到这一点,而是生成的iage到处都有小红线。我相信在代码的某个地方,h和w发生了变化,因此步幅是不正确的。但我找不到问题出在哪里。谢谢你的帮助

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestImageProcessing
{
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

class TransformImage
{
    public static BitmapSource Transform(BitmapImage input)
    {
        var red = new PixelColor() { Blue = 0, Green = 0, Red =255, Alpha =128 };
        var inputPixels = GetPixels(input);
        var outPixel = new PixelColor[inputPixels.GetLength(0), inputPixels.GetLength(1)];

        var output = new WriteableBitmap(input.PixelWidth, input.PixelHeight, input.DpiX, input.DpiY, input.Format, input.Palette);
        for (int h = 0; h < input.PixelHeight; h++)
        {
            for (int w = 0; w < input.PixelWidth; w++)
            {


               if (h < 100)
               {
                   outPixel[w, h] = red;
               }
               else
               {
                   outPixel[w, h] = inputPixels[w, h];
               }

            }
        }

        PutPixels(output, outPixel);
        return output;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct PixelColor
    {
        public byte Blue;
        public byte Green;
        public byte Red;
        public byte Alpha;
    }

    public static void PutPixels(WriteableBitmap bitmap, PixelColor[,] pixels)
    {
        int width = pixels.GetLength(0);
        int height = pixels.GetLength(1);
        bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * 4, 0);
    }
    public static PixelColor[,] GetPixels(BitmapSource source)
    {
        if (source.Format != PixelFormats.Bgra32)
        {
            source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
        }
       PixelColor[,] pixels = new PixelColor[source.PixelWidth, source.PixelHeight];

        int stride = source.PixelWidth * ((source.Format.BitsPerPixel + 7) / 8);
        GCHandle pinnedPixels = GCHandle.Alloc(pixels, GCHandleType.Pinned);
        source.CopyPixels(
          new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight),
          pinnedPixels.AddrOfPinnedObject(),
          pixels.GetLength(0) * pixels.GetLength(1) * 4,
              stride);
        pinnedPixels.Free();
        return pixels;

    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间测试图像处理
{
使用System.IO;
使用System.Runtime.InteropServices;
使用System.Windows;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
类转换图像
{
公共静态位图源转换(位图图像输入)
{
var red=new PixelColor(){Blue=0,Green=0,red=255,Alpha=128};
var inputPixels=GetPixels(输入);
var outPixel=new PixelColor[inputPixels.GetLength(0),inputPixels.GetLength(1)];
var output=new WriteableBitmap(input.PixelWidth、input.PixelHeight、input.DpiX、input.DpiY、input.Format、input.palete);
对于(int h=0;h
上述代码的一部分来自此帖子:

MainWindow.xaml:

<Window x:Class="TestImageProcessing.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="550" Width="725">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition Height="30px"/>
    </Grid.RowDefinitions>

    <Image x:Name="InImage" Grid.Row="0" VerticalAlignment="Top" Stretch="Uniform" />
    <Image x:Name="OutImage" Grid.Row="1" VerticalAlignment="Top" Stretch="Uniform" />
    <Button x:Name="Process" Content="Button"  Grid.Row="2"   Width="75" Click="Process_Click"/>




</Grid>
</Window>

MainWindow.xaml.cs:

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;

namespace TestImageProcessing
{


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

    private void Process_Click(object sender, RoutedEventArgs e)
    {
        BitmapImage input= new BitmapImage(new Uri(@"C:\tmp\test.jpg"));
       InImage.Source=input;
        OutImage.Source = TransformImage.Transform(input);

    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间测试图像处理
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有作废处理\u单击(对象发送方,路由目标)
{
BitmapImage输入=新的BitmapImage(新Uri(@“C:\tmp\test.jpg”);
InImage.Source=输入;
OutImage.Source=TransformImage.Transform(输入);
}
}
}

您在Transform方法中创建的WriteableBitmap的像素格式应该是PixelFormats.Bgra32,但您使用的是input参数的像素格式。
如果要将像素存储在二维矩阵中,第一个维度表示行数,第二个维度表示列数,因此应以以下方式声明像素矩阵:

PixelColor[,] pixels = new PixelColor[Height, Width];
请注意,您需要交换宽度和高度。

删除了(不正确)

谢谢。更改此选项会导致“输入数组不是有效的秩。”错误。@这是我的错误,对此表示抱歉。我忘了CopyPixels不适用于多维数组,所以忽略这一部分。