如何将Grayscaled图像转换为数组? [英] How Do I Convert A Grayscaled Image To An Array?

查看:61
本文介绍了如何将Grayscaled图像转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定,但我认为灰度图像是一维数组。我已经使用了以下代码,我不想再使用它了。



请另外建议。

I am not sure but i think gray scaled image is a 1d array. I have already used following code and i don't want to use it again.

Please suggest any thing else.

public byte[] convertToArray(Bitmap grayscaledImage)
        {
            
            MemoryStream m1;
            byte[] imagetoArray;
           
            m1 = new MemoryStream();

            grayscaledImage.Save(m1, System.Drawing.Imaging.ImageFormat.Jpeg); m1.Close();
            imagetoArray = m1.ToArray();
            return imagetoArray;
            
        }

推荐答案

请参阅我对该问题的评论。但最有可能的意思是 System.Drawing.Bitmap ,对于这个类,解决方案是使用 System.Drawing.Bitmap.LockBits

http:// msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx [ ^ ]。



第一个链接上的链接上面引用的页面上引用的两个方法;它有一个很好的代码示例。



这里最重要的是不要尝试在循环中使用 GetPixel ,因为这种方法非常慢。这就是在内存中锁定位的全部想法。请注意,您需要了解您正在使用的 PixelFormat 的内存布局。另请参阅: http://msdn.microsoft.com/en-us/library /system.drawing.imaging.pixelformat.aspx [ ^ ]。



-SA
Please see my comment to the question. But its most likely that you mean System.Drawing.Bitmap and for this class the solution is the use of System.Drawing.Bitmap.LockBits:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx[^].

Clink on the link on the first of the two methods referenced on the page referenced above; it has a good code sample.

Most important thing here is not to try to use GetPixel in a loop, as this method is prohibitively slow. That's all the whole idea of locking bits in memory is about. Note that you need to learn the memory layout for the PixelFormat you're using. See also: http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat.aspx[^].

—SA


这篇关于如何将Grayscaled图像转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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