More servicesWindows Live
HomeHotmailSpacesOneCare
 
MSN
Sign in
 
 
Spaces home  Richard S.R. HanPhotosProfileFriendsMore Tools Explore the Spaces community

Richard S.R. Han

Early to bed and early to rise makes a man healthy, wealthy and wise. ─ Benjamin Franklin
5/3/2008

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

4/27/2008

鼻炎

我天生就過敏,記得小時候每天早上起床就是拿衛生紙猛包水餃,嚴重時,鼻字都已經脫皮了還在流鼻水,不然就是在大太陽下面猛打噴嚏,或在圖書館靜悄悄的時候聽到我擤鼻涕的聲音,這問題其實困惱我很久,

不過隨著年紀增長,我也發現不少方法避免,雖然不一定每個人都有效,但各位可以參考看看,

  • 每天吃一顆綜合維他命
  • 每周適當的運動(激烈運動)
  • 空氣太冷時請戴口罩出門或睡覺
  • 房間太潮濕,請開除濕機
  • 想辦法睡覺熱到流汗
  • 用鼻子去吸熱水產生的蒸氣

 

以上都是我經驗整理出來的,不過當你在考試前幾天,都已經火燒屁股的時候還一直打噴嚏流鼻水,這樣實在是很難專心唸書,直接買成藥來吃,其實是不錯的方法,(雖然吃藥是吃毒,但是某些時候寧可吃毒也不要礙到學分不及格)

好吧! 我要說的事,我最常買的就是 伏冒鼻炎錠,市面上其實還有很多類似的成藥,或者可以值耳鼻喉科去跟醫生拿藥都是不錯的方法,但是在情況特殊的時候,直接吃藥是一條恢復正常的捷徑。

 

這裡還是強調: "我不是來賣藥,也不建議大家吃藥,生體有病最好的方法是讓它自然好"

 

image

(普拿疼伏冒鼻炎錠 Panadol Allergy SINUS Caplets)

 

參考資料:

過敏性鼻炎

台灣鼻炎衛教手冊

3/20/2008

Vista SP1 中文版 五月推出

昨天晚上花了一個小時去從MSP的FTP抓下來安裝,感覺電腦速度又便順暢許多,剛剛查了一下SP修正了哪些東西,比較比較重要如以下幾點:

  • 文件復原&開機&複製檔案&移動檔案 速度提升許多
  • 未壓縮的作業系統更新檔將有1GB檔案容量
  • 安裝OS時必須有7GB的空間
  • 支援exFAT 和EFI
  • 大幅提昇提升與目前市售軟體相容度
  • 進入睡眠模式之後,當你按電源鍵或睡眠鍵喚醒時,顯示器請使用鼠標或鍵盤來喚醒

以上,有些尚未測試,如果有誤,會立即修正!

目前我安裝的應該是 SP1 RC 版本,不過我想應該跟正式版差異不大!   期待五月份的推出!!   
(PS: 盜版用戶,可能會有某些問題,請勿嘗試)

3/12/2008

Silverlight

  silverlightCM

   
Video: silverlightCM

   活動場次:  http://www.microsoft.com/taiwan/student/events/msp 

3/10/2008

WretchLeech!

好久沒寫了!
先推薦一個看無名的軟體 WretchLeech

推薦原因:速度快、支援鍵盤快鍵、免安裝、無廣告、好用

WretchLeech! 首頁
下載 WretchLeech: 右鍵直接下載(40k)ZIP 檔(14k)

View more entries
 
No list items have been added yet.