影象顯示與儲存
阿新 • • 發佈:2018-12-26
設計WinForm程式,將圖片fy.jpg以指定大小顯示在窗體上的PictureBox1控制元件上,並將顯示的影象儲存在另一檔案中。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Graphics g; Bitmap b; private void button1_Click_1(object sender, EventArgs e) { g = pictureBox1.CreateGraphics(); b = new Bitmap(Application.StartupPath + @"\fy.jpg"); g.DrawImage(b, 0, 0); b.Dispose(); g.Dispose(); } private void button2_Click(object sender, EventArgs e) { b = new Bitmap(Application.StartupPath + @"\fy.jpg"); Bitmap i = new Bitmap(pictureBox1.Width,pictureBox1.Height); g = Graphics.FromImage(i); g.DrawImage(b,0,0); i.Save(@"D:\fy.jpg",System.Drawing.Imaging.ImageFormat.Gif); b.Dispose(); i.Dispose(); g.Dispose(); } } }