RSS

Category Archives: .NET Framework

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

處理彩色圖片(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,讀出記憶體中的值!!
      本方法適用彩色圖片及灰階圖片!

 
發表迴響

發文者為 於 2008 年 05 月 03 日 英吋 .NET Framework

 

開發平台太人性的缺點

以下程式碼是用VB.NET 寫一個簡單的Application
程式說明:假設已經事前先拉出來兩個 Txtbox 的控制項目(分別命名為 txtX  &  txtY)
                    下面寫了兩個簡單的Function,目的是兩個數字的加總,可以假想是在作加法!!
                    會發現結果一樣,但是執行效能差了N倍!

Function Slow()
       Dim sum As Integer
       For i As Integer = 0 To 100000
           sum = CInt(txtX.Text) + CInt(txtY.Text)
       Next
       Return sum
   End Function

   Function Fast()
       Dim sum As Integer
       Dim a As Integer = txtX.Text
       Dim b As Integer = txtY.Text
       For i As Integer = 0 To 100000
           sum = a + b
       Next
       Return sum
   End Function

做個結論:如果可以的話,不要再大量迴圈內面直接把控制項的資料直接拿來做處理,雖然.NET會自動幫你轉換資料型態,但是效能不是你能想像的!!    

 
發表迴響

發文者為 於 2007 年 08 月 28 日 英吋 .NET Framework

 

.NET 功能用簡單的數學式來說明

.NET 2.0 = CLR +BCL+ C#(VB.NET) + Win Form + Web Form
.NET 3.0 = .NET 2.0 + WCF + WPF + WF + WCS
.NET 3.5 = .NET 3.0 + ASP.NET AJAX +Silverlight+LINQ

透過這個公式,可以把整個 .NET 3.0 & .NET 3.5 簡單的說明,而且又不失重點所在,給大家參考!!

參考資料來源

 
發表迴響

發文者為 於 2007 年 06 月 13 日 英吋 .NET Framework

 

【免費下載】C/C++使用者入門的C#/.NET Framework書籍

.NET Book Zero  (What the C or C++ Programmer Needs to Know About C# and the .NET Framework )

 by  Charles Petzold
 
This free on-line 267-page book is an introduction to C# and the Microsoft .NET Framework for programmers who have experience with C or C++. Version 1.1 of .NET Book Zero was uploaded on January 1, 2007, and contains numerous minor corrections of typos in version 1.0. Version 1.0 of the downloable code was uploaded on December 4, 2006, and has not changed with the new version of the book.
 
發表迴響

發文者為 於 2007 年 03 月 09 日 英吋 .NET Framework