Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C语言中翻转图像#_C# - Fatal编程技术网

C# 如何在C语言中翻转图像#

C# 如何在C语言中翻转图像#,c#,C#,我想翻转这个图像, 这样看来,, 但是我写的代码让它看起来像这样, 我在代码中做错了什么 int Height = TransformedPic.GetLength(0); int Width = TransformedPic.GetLength(1); for (int i = 0; i < Height; i++) { for (int j = 0; j < Width / 2; j++)

我想翻转这个图像,

这样看来,,

但是我写的代码让它看起来像这样,

我在代码中做错了什么

    int Height = TransformedPic.GetLength(0);
        int Width = TransformedPic.GetLength(1);

        for (int i = 0; i < Height; i++)
        {
            for (int j = 0; j < Width / 2; j++)
            {
                TransformedPic[i, j] = TransformedPic[i, ((Width) - (j + 1))];
            }
        }
int Height=TransformedPic.GetLength(0);
int Width=TransformedPic.GetLength(1);
对于(int i=0;i
当您的代码进入图像的一半时,图像的另一半可能已经用第一个更新了

请尝试以下方法:

    var newPic = TransformedPic.Clone(); // Clone your pic to a new object. 
    int Height = TransformedPic.GetLength(0);
    int Width = TransformedPic.GetLength(1);

    for (int i = 0; i < Height; i++)
    {
        for (int j = 0; j < Width / 2; j++)
        {
            newPic[i, j] = TransformedPic[i, ((Width) - (j + 1))];
        }
    }
    TransformedPic = newPic;
var newPic=TransformedPic.Clone();//将图片克隆到新对象。
int Height=TransformedPic.GetLength(0);
int Width=TransformedPic.GetLength(1);
对于(int i=0;i
当您的代码进入图像的一半时,图像的另一半可能已经用第一个更新了

请尝试以下方法:

    var newPic = TransformedPic.Clone(); // Clone your pic to a new object. 
    int Height = TransformedPic.GetLength(0);
    int Width = TransformedPic.GetLength(1);

    for (int i = 0; i < Height; i++)
    {
        for (int j = 0; j < Width / 2; j++)
        {
            newPic[i, j] = TransformedPic[i, ((Width) - (j + 1))];
        }
    }
    TransformedPic = newPic;
var newPic=TransformedPic.Clone();//将图片克隆到新对象。
int Height=TransformedPic.GetLength(0);
int Width=TransformedPic.GetLength(1);
对于(int i=0;i
  • 你覆盖了数据,它们就会丢失
  • 你找错方向了
  • 这应该起作用:

            int Height = TransformedPic.GetLength(0);
            int Width = TransformedPic.GetLength(1);
    
            for (int i = 0; i < Height / 2; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    var tmp = TransformedPic[i, j];
                    TransformedPic[i, j] = TransformedPic[Height - 1 - i, j];
                    TransformedPic[Height - 1 - i, j] = tmp;
                }
            }
    
    int Height=TransformedPic.GetLength(0);
    int Width=TransformedPic.GetLength(1);
    对于(int i=0;i
  • 你覆盖了数据,它们就会丢失
  • 你找错方向了
  • 这应该起作用:

            int Height = TransformedPic.GetLength(0);
            int Width = TransformedPic.GetLength(1);
    
            for (int i = 0; i < Height / 2; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    var tmp = TransformedPic[i, j];
                    TransformedPic[i, j] = TransformedPic[Height - 1 - i, j];
                    TransformedPic[Height - 1 - i, j] = tmp;
                }
            }
    
    int Height=TransformedPic.GetLength(0);
    int Width=TransformedPic.GetLength(1);
    对于(int i=0;i
    如果
    开始
    结束
    是一个范围的包含边,那么典型的(容易记忆的)翻转(或反转)算法如下

    for (int lo = start, hi = end; lo < hi; lo++, hi--)
        swapelements(lo, hi);
    
    水平翻转

    for (int i = 0; i < height; i++)
        for (int lo = 0, hi = width - 1; lo < hi; lo++, hi--)
            Swap(ref TransformedPic[i, lo], ref TransformedPic[i, hi]); 
    
    for(int i=0;i
    垂直翻转(如在问题中)

    for(int i=0;i

    最后,值得一提的是,您可以使用来实现相同的效果,而无需将图像像素放入数组缓冲区并自行处理。

    如果
    开始
    结束
    是一个范围的包含边,那么典型的(容易记忆的)翻转(或反转)算法如下所示

    for (int lo = start, hi = end; lo < hi; lo++, hi--)
        swapelements(lo, hi);
    
    水平翻转

    for (int i = 0; i < height; i++)
        for (int lo = 0, hi = width - 1; lo < hi; lo++, hi--)
            Swap(ref TransformedPic[i, lo], ref TransformedPic[i, hi]); 
    
    for(int i=0;i
    垂直翻转(如在问题中)

    for(int i=0;i

    最后,值得一提的是,您可以使用来实现相同的功能,而无需将图像像素放入数组缓冲区并自行处理。

    创建一个只有8×8像素的图像,将每个像素初始化为不同的值,然后使用该测试用例调试您的程序。在调试器的每一步中,预测将会发生什么和应该发生什么,然后你就会发现你的bug。创建一个只有8×8像素的图像,将每个像素初始化为不同的值,然后用该测试用例调试你的程序。在调试器的每一步中,预测将发生什么与应该发生什么,您就会发现您的错误。不确定TransformedPic
    的时间,但通常索引器是宽度、高度。从OPs电流输出来看,似乎是这样。此代码将水平翻转,而不是垂直翻转,如果
    Width!=高度
    。它应该是:
    newPic[j,i]=TransformedPic[j,Height-i-1]
    j
    的循环应该是
    Width
    ,而不是
    Width/2
    @Rob为什么使用Transformed[j,i]而不是TransformedPic[i,j]?而TransformedPic是加载到我的程序中的原始图像的副本,我只想更改TransformedPic,因为我的程序中还有其他进程也在使用它。@CoolCat,因为索引器通常要求
    [x,y]
    ,其中
    x
    是水平位置,
    y
    是垂直位置。但是,
    i
    是您的垂直位置(当您循环到高度时),因此它不应存储在
    x
    中。你能告诉我TransformedPic是什么类型的吗?@Frank Fajardo我得到一个错误,“当我在循环中写入newPic[I,j]时,不能对文件名类型为'object'的表达式应用[]索引。我如何解决这个问题?@Rob TransformedPic是数组原始副本,它是原始副本