private string ImageFileName = null;下面的定義在壹個按鈕裏面,用來打開圖片。//當然妳要打開圖片才行哈。 OpenFileDialog OpenDlg = new OpenFileDialog(); if (OpenDlg.ShowDialog() == DialogResult.OK)
{
ImageFileName = OpenDlg.FileName;
curImage = Image.FromFile(ImageFileName);
}再在另外的按鈕上面創建壹張圖片,用來保存新圖片。Bitmap bitmap=new Bitmap(100,120);bitmap.Save("xx.jpg",ImageFormat.Jpeg); //這樣保存在當前目錄,並且指定了只能是這個格式的,當然可以設置其他格式的,下面有完整代碼。//////////關於變化圖片大小。///////using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;namespace 使用不同大小保存圖片1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private Image curImage = null;
private string ImageFileName = null;
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog OpenDlg = new OpenFileDialog();
OpenDlg.Filter = "All ImageFile|*.jpg;*.bmp;*.gif;*.png|(*.bmp)|*.bmp|(*.jpg)|*.jpg|(*.png)|*.png|(*.gif)|*.gif";
OpenDlg.Title = "Open Image File"; if (OpenDlg.ShowDialog() == DialogResult.OK)
{
ImageFileName = OpenDlg.FileName;
curImage = Image.FromFile(ImageFileName);
}
this.Invalidate();
} private void Form1_Paint(object sender, PaintEventArgs e)
{
if (curImage != null)
{
e.Graphics.DrawImage(curImage, 0, 0);
}
} private void button1_Click(object sender, EventArgs e)
{
try
{
if (this.textBox1.Text == "" || this.textBox2.Text == "")
{
MessageBox.Show("請輸入大小完整");
}
else
{
int width = Convert.ToInt32(textBox1.Text);
int height = Convert.ToInt32(textBox2.Text); SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "(All ImageFile)|*.jpg;*.png;*.gif;*.bmp";
saveDlg.Title = "Save ImageFile";
saveDlg.OverwritePrompt = true;
if (saveDlg.ShowDialog() == DialogResult.OK)
{
string filename=saveDlg.FileName;
string extn = filename.Remove(0, saveDlg.FileName.Length - 3); Bitmap newBitmap = new Bitmap(curImage, new Size(width, height)); if (extn.Equals("jpg"))
newBitmap.Save(saveDlg.FileName, ImageFormat.Jpeg);
else if (extn.Equals("png"))
newBitmap.Save(saveDlg.FileName, ImageFormat.Png);
else if (extn.Equals("bmp"))
newBitmap.Save(saveDlg.FileName, ImageFormat.Bmp);
else if (extn.Equals("gif"))
newBitmap.Save(saveDlg.FileName, ImageFormat.Gif);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}、、、、、、、、、、、、、、、上面設置的大小是通過使用了,兩個TextBox來設置的。設計代碼(略)。