欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

读取图片像素的具体实例

时间:2020-10-08 13:28:36|栏目:.NET代码|点击:

复制代码 代码如下:

   public static short[][] GetPixs(Bitmap bitmap)
        {
            int height = bitmap.Height;
            int width = bitmap.Width;
            byte tempB, tempG, tempR;
            short[][] spOriginData = new short[height][];
            for (int i = 0; i < height; i++)
            {
                spOriginData[i] = new short[width];
            }

            BitmapData dataOut = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int offset = dataOut.Stride - dataOut.Width * 3;
            try
            {
                unsafe
                {
                    byte* pOut = (byte*)(dataOut.Scan0.ToPointer());

                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            tempB = pOut[0];
                            tempG = pOut[1];
                            tempR = pOut[2];
                            double data=0.31 * tempR + 0.59 * tempG + 0.11 * tempB;
                            if (data > 255)
                                spOriginData[y][x] = 255;
                            else
                                if (data < 0)
                                    spOriginData[y][x] = 0;
                                else
                                    spOriginData[y][x] = (short)data;
                            pOut += 3;
                        }
                        pOut += offset;
                    }
                    bitmap.UnlockBits(dataOut);
                }
            }
            catch
            {
            }
            return spOriginData;
        }      
         

上一篇:用C#的params关键字实现方法形参个数可变示例

栏    目:.NET代码

下一篇:ASP.NET MVC 5使用X.PagedList.Mvc进行分页教程(PagedList.Mvc)

本文标题:读取图片像素的具体实例

本文地址:http://www.codeinn.net/misctech/8629.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有