แลปนี้จะโหลดภาพมาแสดงผลบน From และ จัดภาพ หมุนภาพ เขียนข้อความลงบนภาพเป็นต้น
- การโหลดภาพเพื่อแสดงผลบนFrom
private void Form1_Paint(object sender, PaintEventArgs e)
{
//load bitmap
Bitmap bmp = new Bitmap("C:\\Users\\Priv8\\Pictures\\1.jpg");
//ปรับขนาดสี่เหลี่ยมพิ้นผ้าที่แสดงผลบนจอของภาพ
this.SetClientSizeCore(bmp.Width + 20, bmp.Height + 20);
//วาดภาพ
e.Graphics.DrawImage(bmp, 10, 10);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//load pic
Bitmap bmp = new Bitmap("C:\\Users\\Administrator\\Downloads\\1.jpg");
//สร้างภาพที่ขนาดเล็กลงจากเดิมครึ่งหนึ่ง
Rectangle destrect = new Rectangle(10, 10, bmp.Width / 2, bmp.Height / 2);
//สร้างภาพที่ขนาดเเท่าเดิม
Rectangle srcrect = new Rectangle(10, 10, bmp.Width, bmp.Height);
//ขยายขอบของfromให้ใหญ่กว่าภาพ ด้านละ20
this.SetClientSizeCore(destrect.Width + 20, destrect.Height + 20);
//เขียนภาพที่ขนาดเล็กลงจากเดิมครึ่งหนึ่ง(ภาพ zoom out)
e.Graphics.DrawImage(bmp, destrect, srcrect, GraphicsUnit.Pixel);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//load pic
Bitmap bmp = new Bitmap("C:\\Users\\Administrator\\Downloads\\1.jpg");
//สร้างภาพที่ขนาดเเท่าเดิม
Rectangle destrect = new Rectangle(10, 10, bmp.Width, bmp.Height);
//สร้างภาพที่ขนาดเล็กลงจากเดิมครึ่งหนึ่ง
Rectangle srcrect = new Rectangle(10, 10, bmp.Width/2, bmp.Height/2);
//ขยายขอบของfromให้ใหญ่กว่าภาพ ด้านละ20
this.SetClientSizeCore(destrect.Width + 20, destrect.Height + 20);
//เขียนภาพที่ขนาดเล็กลงจากเดิมครึ่งหนึ่ง(ภาพ zoom in)
e.Graphics.DrawImage(bmp, destrect, srcrect, GraphicsUnit.Pixel);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//load pic
Bitmap bmp = new Bitmap("C:\\Users\\Administrator\\Downloads\\1.jpg");
this.SetClientSizeCore(bmp.Width, bmp.Height);
Rectangle topleft = new Rectangle(0, 0, bmp.Width / 2, bmp.Height / 2);
Rectangle topright = new Rectangle(bmp.Width / 2, 0, bmp.Width / 2, bmp.Height / 2);
Rectangle bottomleft = new Rectangle(0, bmp.Height / 2, bmp.Width / 2, bmp.Height / 2);
Rectangle bottomright = new Rectangle(bmp.Width / 2, bmp.Height / 2, bmp.Width / 2, bmp.Height / 2);
bmp.RotateFlip(RotateFlipType.RotateNoneFlipNone);
e.Graphics.DrawImage(bmp, topleft);
bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);
e.Graphics.DrawImage(bmp, topright);
bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
e.Graphics.DrawImage(bmp, bottomleft);
bmp.RotateFlip(RotateFlipType.Rotate180FlipY);
e.Graphics.DrawImage(bmp, bottomright);
}
- การเขียนข้อความลงในภาพ (โปรแกรมนี้จะเป็นโค้ดของไฟล์From1.cs)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LAB20
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//load pic
Bitmap bmp = new Bitmap("C:\\Users\\Administrator\\Downloads\\1.jpg");
this.SetClientSizeCore(bmp.Width, bmp.Height);
Rectangle destrect = new Rectangle(0, 0, bmp.Width, bmp.Height);
Brush myBrush = new SolidBrush(Color.Coral);
e.Graphics.DrawImage(bmp, destrect);
e.Graphics.DrawString("My sister Cute!!", //string
new Font("Verdana", 30, FontStyle.Bold), // Font , size
myBrush,
0, // x position to display font
0); // y position to display font
}
}
}