Wednesday, May 9, 2012

การจัดการ Image และ PictureBox โดย CSharp ตอนที่ 1

ตอนที่ 1: การเปิด image เพื่อแสดงใน picture box
เกริ่นนำ
เหตุผลที่ตั้งหัวข้อไว้กว้างๆ เพราะโค้ดนี้จะเป็นการใช้งานกับ Image File ให้ละเอียดเท่าที่จะทำได้นะครับ สำหรับ Image File หรือไฟล์อื่นๆนั้นเราสามารถบันทึกไว้ใน Database ได้เพื่อที่จะดึงมาใช้งาน บทความชุดนี้จะเริ่มจากการเปิด Image File to Picture Box ก่อนนะครับ ตัวอย่างมีเยอะแยะแต่อยากให้เริ่มเข้าใจที่มาที่ไปก่อนมาดูโค้ดกันเลย
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private void button11_Click(object sender, EventArgs e)
{
try
{
FileDialog fldlg = new OpenFileDialog();
//specify your own initial directory
fldlg.InitialDirectory = @":D\";
//this will allow only those file extensions to be added
fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
if (fldlg.ShowDialog() == DialogResult.OK)
{
imagename = fldlg.FileName;
filename = fldlg.FileName;
Bitmap newimg = new Bitmap(imagename);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = (Image)newimg;
}
fldlg = null;
}
catch (System.ArgumentException ae)
{
imagename = " ";
MessageBox.Show(ae.Message.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
จากโค้ดเป็นการโหลดภาพลง Picture Box นะครับ มาดูโค้ดตัวที่ 2 เป็นการโหลดจาก Database to Picture Box กันบ้าง
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
try
{
//On Code Change
string sql = "";
//Select Data First
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
 
sql = "Select * From menuimages Where code=@Code";
SqlCommand command = new SqlCommand(sql, connection);
command.CommandType = CommandType.Text;
//command.CommandType = CommandType.Text;
SqlParameter param0 = new SqlParameter("@Code", SqlDbType.VarChar, 10);
param0.Value = textBox7.Text;
command.Parameters.Add(param0);
connection.Open();
SqlDataReader read = command.ExecuteReader();
 
if (read.HasRows)
{
 
while (read.Read())
{
string id = read["code"].ToString();
Program.frmshowpic.pictureBox1.Image = null;
// Delete a file by using File class static method...
//if (System.IO.File.Exists(@imagename))
//{
//        System.IO.File.Delete(@imagename);
//}
string _imagefile = read["ImageName"].ToString();
if (System.IO.File.Exists(@_imagefile))
{
Bitmap newimg = new Bitmap(_imagefile);
Program.frmshowpic.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Program.frmshowpic.pictureBox1.Image = (Image)newimg;
}
else
{
imagename = "C:/Temp/" + id + ".jpg";
if (System.IO.File.Exists(@imagename))
{
Bitmap newimg = new Bitmap(imagename);
Program.frmshowpic.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Program.frmshowpic.pictureBox1.Image = (Image)newimg;
}
else
{
FileStream FS1 = new FileStream(@"C:\Temp\" + id + ".jpg", FileMode.Create);
imgbin = (byte[])read["imageFile"];
//byte[] blob = (byte[])dataRow[1];
FS1.Write(imgbin, 0, imgbin.Length);
FS1.Close();
FS1 = null;
Program.frmshowpic.pictureBox1.Image = Image.FromFile(@"C:\Temp\" + id + ".jpg");
Program.frmshowpic.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
Program.frmshowpic.pictureBox1.Refresh();
}
}
}
}
else
{
Program.frmshowpic.pictureBox1.Image = null;
}
read.Close();
connection.Close();
}
catch (ApplicationException ex)
{
MessageBox.Show(ex.ToString());
}
จากตัวอย่างเป็นโค้ดที่ใช้งานจริงเวลานำไปใช้ก็ตัดโค้ดที่ไม่จำเป็นออกได้ ก็จบในส่วนของการโหลดภาพลง Picture Box ทั้งจากไฟล์และก็จาก Database

No comments:

Post a Comment