C#初體驗,畫圖的讀、寫、顯示

Visual Studio C# 2010 Express
WindowsFormsApplication專案

將檔案讀進來顯示、編輯
  • Image.FromFile(str); (讀取)
  • Image→PictureBox.Image(顯示)
  • Image→Graphics(編輯)
Image image = Image.FromFile(strNamePath);   //從檔案讀取
pictureBox.Image = image;                    //顯示在控制項Picture Box上
Graphics graph = Graphics.FromImage(image);  //開放編輯


建立檔案編輯、顯示後存檔
  • Bitmap→graph(編輯)
  • Bitmap→Picture.Image(顯示)
  • Bitmap.save(); (存檔)
Bitmap bmp = new Bitmap(200, 200);
Graphics graph = Graphics.FromImage(bmp);  //開放編輯權限
/* 編輯... */
pictureBox.Image = bmp;                    //顯示
bmp.Save(strNamePath, System.Drawing.Imaging.ImageFormat.Bmp);  //存檔


讀檔之後再存檔
  • Image.FromFile(str); (讀取)
  • Bitmap(Image)(轉格式)
  • Bitmap.save(); (存檔)
Image image = Image.FromFile(strOpenPathName);
Bitmap bmp = new Bitmap(image);
bmp.Save(strSavePathName, System.Drawing.Imaging.ImageFormat.Bmp);


讀檔、編輯、存檔
這個方法,畫出來的內容會被讀進來的檔案蓋住!要注意唷!
  • Image.FromFile(str); (讀取)
  • Bitmap(Image)(轉格式)
  • Bitmap.save(); (存檔)
// Create a Bitmap object from a file.
Bitmap myBitmap = new Bitmap("Grapes.jpg");
         
// Draw myBitmap to the screen.
e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width,
    myBitmap.Height);
         
// Set each pixel in myBitmap to black.
for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
{
    for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
    {
        myBitmap.SetPixel(Xcount, Ycount, Color.Black);
    }
}
         
// Draw myBitmap to the screen again.
e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0,
    myBitmap.Width, myBitmap.Height);
參考自: Bitmap.SetPixel 方法

沒有留言:

張貼留言

(什麼是留言欄訊息?)