pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

c# 位圖數(shù)據(jù)中的 RGB 值

c# RGB Values from Bitmap Data(c# 位圖數(shù)據(jù)中的 RGB 值)
本文介紹了c# 位圖數(shù)據(jù)中的 RGB 值的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我是位圖的新手,每像素使用 16 位Format16bppRgb555

I am new in working with Bitmap and using 16 bits per pixel Format16bppRgb555;

我想從位圖數(shù)據(jù)中提取 RGB 值.這是我的代碼

I want to Extract RGB Values from Bitmap Data. Here is my code

static void Main(string[] args)
    {

        BitmapRGBValues();
        Console.ReadKey();
    }


 static unsafe void BitmapRGBValues()
    {

        Bitmap cur = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format16bppRgb555);
        //Capture Screen
        var screenBounds = Screen.PrimaryScreen.Bounds;
        using (var gfxScreenshot = Graphics.FromImage(cur))
        {
            gfxScreenshot.CopyFromScreen(screenBounds.X, screenBounds.Y, 0, 0, screenBounds.Size, CopyPixelOperation.SourceCopy);
        }

        var curBitmapData = cur.LockBits(new Rectangle(0, 0, cur.Width, cur.Height),
                                 ImageLockMode.ReadWrite, PixelFormat.Format16bppRgb555);


        try
        {
            byte* scan0 = (byte*)curBitmapData.Scan0.ToPointer();

            for (int y = 0; y < cur.Height; ++y)
            {
                ulong* curRow = (ulong*)(scan0 + curBitmapData.Stride * y);

                for (int x = 0; x < curBitmapData.Stride / 8; ++x)
                {

                    ulong pixel = curRow[x];

                    //How to get RGB Values  from pixel;







                }

            }


        }
        catch
        {

        }
        finally
        {
            cur.UnlockBits(curBitmapData);

        }


    }

推薦答案

LockBits 實(shí)際上可以轉(zhuǎn)換您的圖像到所需的像素格式,這意味著不需要進(jìn)一步轉(zhuǎn)換.只需將圖像鎖定為 Format32bppArgb,您就可以簡(jiǎn)單地從單個(gè)字節(jié)中獲取顏色值.

LockBits can actually convert your image to a desired pixel format, meaning no further conversion should be needed. Just lock the image as Format32bppArgb and you can simply take your colour values from single bytes.

BitmapData curBitmapData = cur.LockBits(new Rectangle(0, 0, cur.Width, cur.Height),
    ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
Int32 stride = curBitmapData.Stride;
Byte[] data = new Byte[stride * cur.Height];
Marshal.Copy(curBitmapData.Scan0, data, 0, data.Length);
cur.UnlockBits(curBitmapData);

使用此代碼,您最終會(huì)得到字節(jié)數(shù)組 data,其中填充了 ARGB 格式的圖像數(shù)據(jù),這意味著顏色分量字節(jié)將按 [B, G, R, A].請(qǐng)注意,stride 是跳轉(zhuǎn)到圖像下一行的字節(jié)數(shù),因?yàn)檫@總是等于width * bytes per pixel",應(yīng)始終予以考慮.

With this code, you end up with the byte array data, which is filled with your image data in ARGB format, meaning the colour component bytes will be in there in the order [B, G, R, A]. Note that the stride is the amount of bytes to skip to get to a next line on the image, and since this is not always equal to "width * bytes per pixel", it should always be taken into account.

現(xiàn)在你明白了,你可以用它做任何你想做的事......

Now you got that, you can do whatever you want with it...

Int32 curRowOffs = 0;
for (Int32 y = 0; y < cur.Height; y++)
{
    // Set offset to start of current row
    Int32 curOffs = curRowOffs;
    for (Int32 x = 0; x < cur.Width; x++)
    {
        // ARGB = bytes [B,G,R,A]
        Byte b = data[curOffs];
        Byte g = data[curOffs + 1];
        Byte r = data[curOffs + 2];
        Byte a = data[curOffs + 3];
        Color col = Color.FromArgb(a, r, g, b);

        // Do whatever you want with your colour here
        // ...

        // Increase offset to next colour
        curOffs += 4;
    }
    // Increase row offset
    curRowOffs += stride;
}

您甚至可以編輯字節(jié),然后根據(jù)需要根據(jù)它們構(gòu)建新圖像.

You can even edit the bytes, and then build a new image from them if you want.

這篇關(guān)于c# 位圖數(shù)據(jù)中的 RGB 值的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應(yīng)用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動(dòng))
C# Console app - How do I make an interactive menu?(C# 控制臺(tái)應(yīng)用程序 - 如何制作交互式菜單?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創(chuàng)建重復(fù)的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動(dòng)化控制桌面應(yīng)用程序并單擊菜單條)
Removing thin border around the menuitems(刪除菜單項(xiàng)周?chē)募?xì)邊框)
主站蜘蛛池模板: 国资灵活用工平台_全国灵活用工平台前十名-灵活用工结算小帮手 | 高温链条油|高温润滑脂|轴承润滑脂|机器人保养用油|干膜润滑剂-东莞卓越化学 | 隔离变压器-伺服变压器--输入输出电抗器-深圳市德而沃电气有限公司 | 莱州网络公司|莱州网站建设|莱州网站优化|莱州阿里巴巴-莱州唯佳网络科技有限公司 | 杭州用友|用友软件|用友财务软件|用友ERP系统--杭州协友软件官网 | 合肥防火门窗/隔断_合肥防火卷帘门厂家_安徽耐火窗_良万消防设备有限公司 | 运动木地板厂家_体育木地板安装_篮球木地板选购_实木运动地板价格 | 微波萃取合成仪-电热消解器价格-北京安合美诚科学仪器有限公司 | 影合社-影视人的内容合作平台 | 锂电池砂磨机|石墨烯砂磨机|碳纳米管砂磨机-常州市奥能达机械设备有限公司 | 烟气在线监测系统_烟气在线监测仪_扬尘检测仪_空气质量监测站「山东风途物联网」 | [官网]叛逆孩子管教_戒网瘾学校_全封闭问题青少年素质教育_新起点青少年特训学校 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 成都思迪机电技术研究所-四川成都思迪编码器 | 电车线(用于供电给电车的输电线路)-百科| 注塑_注塑加工_注塑模具_塑胶模具_注塑加工厂家_深圳环科 | 上海阳光泵业制造有限公司 -【官方网站】 | 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 | 骨密度仪-骨密度测定仪-超声骨密度仪-骨龄测定仪-天津开发区圣鸿医疗器械有限公司 | 立式硫化罐-劳保用品硫化罐-厂家直销-山东鑫泰鑫硫化罐厂家 | 照相馆预约系统,微信公众号摄影门店系统,影楼管理软件-盟百网络 | 威实软件_软件定制开发_OA_OA办公系统_OA系统_办公自动化软件 | 网站建设-临朐爱采购-抖音运营-山东兆通网络科技 | 不锈钢复合板|钛复合板|金属复合板|南钢集团安徽金元素复合材料有限公司-官网 | 智能家居全屋智能系统多少钱一套-小米全套价格、装修方案 | jrs高清nba(无插件)直播-jrs直播低调看直播-jrs直播nba-jrs直播 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | POM塑料_PBT材料「进口」聚甲醛POM杜邦原料、加纤PBT塑料报价格找利隆塑料 | 消泡剂_水处理消泡剂_切削液消泡剂_涂料消泡剂_有机硅消泡剂_广州中万新材料生产厂家 | 南京泽朗生物科技有限公司 | 北京模型公司-工业模型-地产模型-施工模型-北京渝峰时代沙盘模型制作公司 | 一体化净水器_一体化净水设备_一体化水处理设备-江苏旭浩鑫环保科技有限公司 | 辐射仪|辐射检测仪|辐射巡测仪|个人剂量报警仪|表面污染检测仪|辐射报警仪|辐射防护网 | 517瓜水果特产网|一个专注特产好物的网站 | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 河南膏药贴牌-膏药代加工-膏药oem厂家-洛阳今世康医药科技有限公司 | 电子海图系统-电梯检验系统-智慧供热系统开发-商品房预售资金监管系统 | 防火门|抗爆门|超大门|医疗门|隔声门-上海加汇门业生产厂家 | 金属雕花板_厂家直销_价格低-山东慧诚建筑材料有限公司 | 集装箱展厅-住人集装箱住宿|建筑|房屋|集装箱售楼处-山东锐嘉科技工程有限公司 | 山东信蓝建设有限公司官网 |