Bitmap bitmap;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "*.bmp|*.bmp";
openfile.Title = "open 文件";
openfile.Multiselect = false;
if (openfile.ShowDialog()==DialogResult.OK)
{
if (bitmap != null)
bitmap.Dispose();
string fileName = openfile.FileName;
bitmap = new Bitmap(fileName);
if (bitmap.Width>bitmap.Height)
{
pictureBox1.Width = panel1.Width;
pictureBox1.Height = (int)((double)bitmap.Height * panel1.Width / bitmap.Width);
}
else
{
pictureBox1.Height = panel1.Height;
pictureBox1.Width = (int)((double)bitmap.Width * panel1.Height / bitmap.Height);
}
pictureBox1.Image = bitmap;
FileInfo f = new FileInfo(fileName);
this.Text = "圖像轉換" + f.Name;
button2.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem==null)
{
return;
}
else
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "轉化為: ";
saveFileDialog.OverwritePrompt = true;
saveFileDialog.CheckPathExists = true;
saveFileDialog.Filter = comboBox1.Text + "|" + comboBox1.Text;
if (saveFileDialog.ShowDialog()==DialogResult.OK)
{
string fileName = saveFileDialog.FileName;
bitmap.Save(fileName, ImageFormat.Jpeg);
FileInfo f = new FileInfo(fileName);
this.Text = "圖像轉換: " + f.Name;
label1.Text = f.Name;
}
}
}
圖像轉換