วันศุกร์ที่ 7 มิถุนายน พ.ศ. 2556

เขียนโปรแกรม C# .net บันทึกรูปเป็นไฟล์โดยใช้ picturebox

หลายโปรแกรม C# .net ที่จะพัฒนาส่วนใหญ่จะมีการทำงานเกี่ยวกับรูป ไม่ว่าจะเป็นรูปสินค้า รูปพนักงาน ดังนี้ ครั้งนี้ผมจะแนะนำให้ท่านผู้อ่านใช้ Controlที่ชื่อว่า picturebox บันทึกรูปเป็นไฟล์
ทำไมต้องบันทึกเป็นไฟล์ ทำไมไม่บันทึกลงฐานข้อมูล อ่าเป็นคำถามที่ดีมาครับ(ถามเองตอบเอง)
ก็เพราะว่าง่ายต่อการจัดการ และฐานข้อมูลไม่ใหญ่เกินไป

มาลงมือกันเลยครับ!!

- สร้างฟอร์มดังรูปด้านล่าง

- เรียกใช้ Namespace เพิ่มดังนี้ 

using System.Drawing.Imaging;
using System.IO;


- เขียนโค้ดตามด้านล่างนี้
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;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.ImageLocation = @"C:\Users\Miki\Downloads\Insert Retrieve Images From MySQL\Insert Retrieve Images From MySQL\CSharpMySQLBLOBImages\CSharpMySQLBLOBImages\bin\Debug\Images\Image_4.jpg";
   
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists("Images"))
            {
                Directory.CreateDirectory("Images");
            }

            pictureBox1.Image.Save(@"Images\Image_4_111.jpg", ImageFormat.Jpeg);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(pictureBox1.Image);
            Graphics gr = Graphics.FromImage(bmp);
            Pen p = new Pen(Color.Red);
            p.Width = 5.0f;
            gr.DrawRectangle(p, 1, 2, 30, 40);
            pictureBox1.Image = bmp;
        }
    }
}


ไม่มีความคิดเห็น:

แสดงความคิดเห็น