2010-12-27 10:54:24Chris C.S Huang

[C#] 將影像轉為Byte Array

[C#] 載入影像檔一文中提到,C# 語言可以使用

Bitmap myBitmap = new Bitmap(ImageFileName) 載入影像檔.

並以Bitmap類型的GetPixel(x,y)及SetPixel(x,y,color)兩個函數,對影像的每一個

像素的R, G, B值做讀出和寫入的動作。但如果對一張百萬像素的影像做影像

處理,則必須重複呼叫上述兩個函式上百萬次,這種方式對程式而言顯得太沒有效率。


 C#的Bitmap類型另提供一LockBits()函式可將影像的指定區域資料以Byte Array的型式存放在記憶體空間中,。並傳回BitmapData類型的物件,其中BitmapData.Scan0則存放像素Byte Array中第一個Byte的指標。 程式可以讀取

指標內容的方式,將影像像素資料逐一讀入預定的像素陣列中並進行後續的影像處理,以加快速度。

語法如下:

Bitmap myBitmap =  new Bitmap( ImageFileName );

BitmapData byteArray = myBitmap.LockBits( new Rectangle( 0 , 0 , myBitmap.Width , myBitmap.Height ) , ImageLockMode.ReadWrite  , PixelFormat.Format24bppRgb  );

 unsafe   //進行指標處理所須宣告。專案->屬性->建置->容許Unsafe程式碼須選取。 
            {
                byte* imgPtr = (byte*)(byteArray.Scan0);
                for (int y = 0; y < byteArray.Height; y++)
                {
                    for (int x = 0; x < byteArray.Width; x++)
                    {
                        ImgData[x, y, 2] = (int) *(imgPtr);
                        ImgData[x, y, 1] = (int) *(imgPtr + 1);
                        ImgData[x, y, 0] = (int) *(imgPtr + 2);
                        imgPtr += 3;
                    }
                    imgPtr += ByteOfSkip;
                }
            }

範例程式:

讀入影像檔轉為像素資料陣列,並進行灰階(Grey Scale) 處理.

 

程式碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Bitmap myBitmap = null;
        private void button1_Click(object sender, EventArgs e)     ////載入圖檔
        {
            this.openFileDialog1.Filter = "所有檔案|*.*|BMP File| *.bmp|JPEG File|*.jpg| GIF File|*.gif";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)   ////由對話框選取圖檔
            {
                myBitmap = new Bitmap(openFileDialog1.FileName);
                pictureBox1.Image = myBitmap;
            }

               pictureBox2.Image = null;
        }

        private void button2_Click(object sender, EventArgs e)  ///灰階影像處理
        {
            int[, ,] ImgData = GetImgData(myBitmap);
            GrayProcess(ImgData);
            Bitmap processedBitmap = CreateBitmap(ImgData);
            pictureBox2.Image = processedBitmap;

        }
        private int[, ,] GetImgData(Bitmap myBitmap)
        {
            int[,,] ImgData = new int[myBitmap.Width, myBitmap.Height, 3];
            BitmapData byteArray = myBitmap.LockBits(new Rectangle(0, 0, myBitmap.Width, myBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            int ByteOfSkip = byteArray.Stride - byteArray.Width * 3;
            unsafe  //專案->屬性->建置->容許Unsafe程式碼須選取。           

    {
                byte* imgPtr = (byte*)(byteArray.Scan0);
                for (int y = 0; y < byteArray.Height; y++)
                {
                    for (int x = 0; x < byteArray.Width; x++)
                    {
                        ImgData[x, y, 2] = (int) *(imgPtr);
                        ImgData[x, y, 1] = (int) *(imgPtr + 1);
                        ImgData[x, y, 0] = (int) *(imgPtr + 2);
                        imgPtr += 3;
                    }
                    imgPtr += ByteOfSkip;
                }
            } 

            myBitmap.UnlockBits(byteArray);
            return ImgData;
        }

        public static Bitmap CreateBitmap(int[, ,] ImgData)
        {   
            int Width = ImgData.GetLength(0);
            int Height = ImgData.GetLength(1);
            Bitmap myBitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);

            BitmapData byteArray = myBitmap.LockBits(new Rectangle(0, 0, Width, Height),
                                           ImageLockMode.WriteOnly,
                                           PixelFormat.Format24bppRgb);
         
            //Padding bytes的長度
            int ByteOfSkip = byteArray.Stride - myBitmap.Width * 3;

            unsafe       
            {                                   // 指標取出影像資料
                byte* imgPtr = (byte*) byteArray.Scan0;
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        *imgPtr = (byte)ImgData[x, y, 2];       //B
                      
                        *(imgPtr+1) = (byte)ImgData[x, y, 1];   //G
                      
                        *(imgPtr+2) = (byte)ImgData[x, y, 0];   //R 
                        imgPtr += 3;
                    }
                    imgPtr += ByteOfSkip; // 跳過Padding bytes
                }
            }
            myBitmap.UnlockBits(byteArray);
            return myBitmap;
        }

        private void GrayProcess(int[, ,] ImgData)
        {
            int Width = ImgData.GetLength(0);
            int Height = ImgData.GetLength(1);
          
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {

                    int gray = (int) ((double) ImgData[x, y, 0] * 0.299  + (double) ImgData[x, y, 1] * 0.587 + (double) ImgData[x, y, 2] * 0.114);

                    ImgData[x, y, 0] = gray;
                    ImgData[x, y, 1] = gray;
                    ImgData[x, y, 2] = gray;

                }
            }
        }

    }
}



參考資料:

Basic Image Processing support in C#

簡單的數位影像處理 ( C# 篇)

路人乙 2020-06-22 16:04:33

真的還是unsafe法快好多,以前用VB6時都要等好久

Mark 2012-11-27 02:36:19

您好,看完您的程式範例後,有一延伸問題想請教:
如有一圖片原本即為灰階影像,
將其像素值暫存於一矩陣中,
並將所有像素值已Chart方式顯示於from之上的話,
該如何撰寫程式!?

感謝您!! :)

路人甲 2011-08-27 17:56:09

請問大大原來就是灰階影像的圖片~要讀取圖的灰階值並把它存到一個矩陣!要如何做???

版主回應
不是很瞭解您的問題.
基本上, 灰階影像(jpg, bmp檔)的讀取方式與和彩色影像一樣, 只是影像內容的 R = G = B = Y
2011-08-31 13:50:49