在C#中的倒影 [英] inverted image in C#

查看:128
本文介绍了在C#中的倒影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的一段代码,我从这里 聚集。

This is my piece of code that i gathered from here.

private unsafe byte[] BmpToBytes_Unsafe(Bitmap bmp)

        {

            BitmapData bData = bmp.LockBits(new Rectangle(0,0,1000,1000),

                ImageLockMode.ReadOnly,

                PixelFormat.Format24bppRgb);

            // number of bytes in the bitmap



            int byteCount = bData.Stride * bmp.Height;

            byte[] bmpBytes = new byte[byteCount];



            // Copy the locked bytes from memory





           Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount);

            Marshal.



            // don't forget to unlock the bitmap!!

            bmp.UnlockBits(bData);



            return bmpBytes;

        }



我有上面提到的功能得到字节的函数,只是显示它没有进一步处理。但我得到的倒影在输出端。有人能解释一下吗?

I have a function that gets byte from above mentioned function and just displays it without further processing. But i get the inverted image at the output. Can somebody explain?

推荐答案

当你说倒,我想,你的意思是倒挂?

When you say "inverted", I suppose that you mean upside down?

您不能依靠张贴的代码的人的的l33t hax0r的skillz。他缺乏有关的位图是如何在内存中处理的一些重要信息。

You can't rely on the "l33t hax0r skillz" of the person posting that code. He lacks some vital information about how bitmaps are handled in memory.

当您阅读从位图中的数据不能在一大块读这一切。的数据存储在线和线既可以与顶部线的第一或底行首先被存储。此外,有可能是线路之间填充,以得到一个连字边界上的每一行。

When you read the data from the bitmap you can't read it all in one chunk. The data is stored in lines, and the lines may be stored either with the top line first or the bottom line first. Also, there may be padding between the lines to get each line on an even word boundary.

SCAN0 属性是一个指针到第一行的开始,和在属性的偏移到下一行的开始。在宽度属性可以用来确定有多少数据是在每行。

The Scan0 property is a pointer to the start of the first line, and the Stride property is the offset to the start of the next line. The Width property can be used to determine how much data there is in each line.

所以,你必须一行复制数据在同一时间:

So, you have to copy the data one line at a time:

int lineSize = bData.Width * 3;
int byteCount = lineSize * bData.Height;
byte[] bmpBytes = new byte[byteCount];

IntPtr scan = bData.Scan0;
for (int i = 0; i < bData.Height; i++) {
  Marshal.Copy(scan, bmpBytes[i * lineSize], 0, lineSize);
  scan += bData.Stride;
}

这篇关于在C#中的倒影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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