题目要求:
编写一个window应用程序,分别利用Bitmap类和PictureBox控件实现显示,保存图像的功能。
输入代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace baocun
{
public partial class Form1 : Form
{
Graphics g;
Bitmap bitmap;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
g = pictureBox1.CreateGraphics();
bitmap = new Bitmap(Application.StartupPath + @"\48.jpg");
g.DrawImage(bitmap, 0, 0);//从图片框的左上角开始绘制图片
bitmap.Dispose();
g.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
bitmap = new Bitmap(Application.StartupPath + @"\48.jpg");
Bitmap image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g = Graphics.FromImage(image);
g.DrawImage(bitmap, 0, 0);
image.Save(@"\48.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
image.Dispose();
g.Dispose();
}
}
}
运行截图: