RSS

影像處理(處理彩色、灰階、大小 、負片) C#

03 五月

處理彩色圖片(C#)

System.Drawing.Bitmap img1 = new System.Drawing.Bitmap(@"C:/pic/original.jpg");

System.Drawing.Imaging.BitmapData bmData = img1.LockBits(new System.Drawing.Rectangle(0, 0, img1.Width, img1.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, img1.PixelFormat);

int stride = bmData.Stride;
unsafe
{
    int nOffset = stride – img1.Width * 3;
    System.IntPtr t = bmData.Scan0 ;
    byte* p = (byte*)(void*)t;               
    for (int y = 0; y < img1.Height   ; y++)
    {
        for (int x = 0; x < img1.Width   ; x++)
        {
            //決定圖片每一個Pixel的RGB值       ex: p[0] p[1] p[2] 共同決定左上角第一個pixel
            p[y * (img1.Width * 3 + nOffset) + (x * 3) + 0] = (byte)(0);          //blue
            p[y * (img1.Width * 3 + nOffset) + (x * 3) + 1] = (byte)(255);        //green
            p[y * (img1.Width * 3 + nOffset) + (x * 3) + 2] = (byte)(0);          //red                     
        }
    }               
}
img1.UnlockBits(bmData);

//決定檔案格式(jpeg,bmp,gif…..)
img1.Save(@"C:\pic\revise.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); 

clip_image002[12]

處理灰階圖片(C#)

System.Drawing.Bitmap img1 = new System.Drawing.Bitmap(@"C:/pic/clown.jpg");

System.Drawing.Imaging.BitmapData bmData = img1.LockBits(new System.Drawing.Rectangle(0, 0, img1.Width, img1.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, img1.PixelFormat);

int stride = bmData.Stride;
unsafe
{              
    System.IntPtr t = bmData.Scan0 ;
    byte* p = (byte*)(void*)t;               
    for (int y = 0; y < img1.Height; y++)
    {
        for (int x = 0; x < img1.Width   ; x++)
        {
            //決定圖片每一個Pixel的灰階值(0~255)
            p[ y * img1.Width + x] = (byte)(255 – p[y * img1.Width + x]);                      
        }
    }               
}
img1.UnlockBits(bmData);

//決定檔案格式(jpeg,bmp,gif…..)
img1.Save(@"C:\pic\revise.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);           

clip_image002[14]

處理圖片大小(C#)
System.Drawing.Bitmap img1 = new System.Drawing.Bitmap(@"C:/pic/original.jpg");           
System.Drawing.Bitmap img2 = new System.Drawing.Bitmap(img1.Width *2, img1.Height*2 );

System.Drawing.Imaging.BitmapData bmData = img1.LockBits(new System.Drawing.Rectangle(0, 0, img1.Width , img1.Height ), System.Drawing.Imaging.ImageLockMode.ReadWrite, img1.PixelFormat);        
System.Drawing.Imaging.BitmapData bmData2 = img2.LockBits(new System.Drawing.Rectangle(0, 0, img2.Width, img2.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, img1.PixelFormat);  
       
int stride = bmData.Stride;
int stride2 = bmData2.Stride;
unsafe
{
    int nOffset = stride – img1.Width * 3;
    int nOffset2 = stride2 – img2.Width * 3;
    System.IntPtr t = bmData.Scan0;
    System.IntPtr t2 = bmData2.Scan0;
    byte* p = (byte*)(void*)t;                                  
    byte* p2 = (byte*)(void*)t2;

    for (int y = 0; y < img2.Height; y++)
    {  
        for (int x = 0; x < img2.Width; x++)
        {
             p2[y * (img2.Width * 3 + nOffset2) + (x * 3) + 0] = (byte) (255);     //blue 底色
        }
    }

    for (int y = 0; y < img1.Height; y++)
    {
        for (int x = 0; x < img1.Width ; x++)
        {   
            p2[y * (img2.Width * 3 + nOffset2) + (x * 3) + 0] = p[y * (img1.Width * 3 + nOffset) + (x * 3) + 0];
            p2[y * (img2.Width * 3 + nOffset2) + (x * 3) + 1] = p[y * (img1.Width * 3 + nOffset) + (x * 3) + 1];       
            p2[y * (img2.Width * 3 + nOffset2) + (x * 3) + 2] = p[y * (img1.Width * 3 + nOffset) + (x * 3) +2];                                  
        }                                    
    }
}
img1.UnlockBits(bmData);
img2.UnlockBits(bmData2);
//決定檔案格式(jpeg,bmp,gif…..)
img2.Save(@"C:\pic\revise2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg );  

 clip_image002[16]

處理圖片彩色轉灰階(C#)   
System.Drawing.Bitmap img1 = new System.Drawing.Bitmap(@"C:/pic/original.jpg");

System.Drawing.Imaging.BitmapData bmData = img1.LockBits(new System.Drawing.Rectangle(0, 0, img1.Width, img1.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, img1.PixelFormat);

int stride = bmData.Stride;
unsafe
{
    int nOffset = stride – img1.Width * 3;
    System.IntPtr t = bmData.Scan0;
    byte* p = (byte*)(void*)t;
    for (int y = 0; y < img1.Height; y++)
    {
        for (int x = 0; x < img1.Width; x++)
        {
            //計算灰階值                       
double gray = 0.114* p[y * (img1.Width * 3 + nOffset) + (x * 3) + 0] + 0.587* p[y * (img1.Width * 3 + nOffset) + (x * 3) + 0] + 0.299* p[y * (img1.Width * 3 + nOffset) + (x * 3) + 0];

            //決定圖片每一個Pixel的RGB值   ex: p[0] p[1] p[2] 共同決定左上角第一個pixel
            p[y * (img1.Width * 3 + nOffset) + (x * 3) + 0] = (byte)(gray);          //blue
            p[y * (img1.Width * 3 + nOffset) + (x * 3) + 1] = (byte)(gray);         //green
            p[y * (img1.Width * 3 + nOffset) + (x * 3) + 2] = (byte)(gray);          //red                     
        }
    }
}
img1.UnlockBits(bmData);

//決定檔案格式(jpeg,bmp,gif…..)
img1.Save(@"C:\pic\revise.jpg", System.Drawing.Imaging.ImageFormat.Jpeg  );  

 

 

 clip_image002[18]

Ø 怎麼允許Unsafe程式碼?

Step1:開啟專案的 [屬性] 頁面。

clip_image003[8]

Step2:按一下 [建置] 屬性頁。

clip_image007[4]

Step3:選取 [容許 Unsafe 程式碼] 核取方塊。

Ø 如何加入參考 System.Drawing?

Step1:

clip_image009[6]

Step2:

clip_image011[4]

Step3

:clip_image013[4]

 

PS: 因為當助教的關係,所以去找一下C#怎麼直接利用指標去對印到每張圖片Pixel,讀出記憶體中的值!!
      本方法適用彩色圖片及灰階圖片!

 
發表留言

Posted by 於 2008 年 05 月 03 日 in .NET Framework

 

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

 
%d 位部落客按了讚: