python bmp转jpg_BMP转jpg的c#代码

文件互转代码如下,留藏备用:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Linq;

using System.Windows.Forms;

using System.IO;

using System.Drawing.Imaging;

namespace JPGChangeBMP

{

public partial class Frm_Main : Form

{

Bitmap bitmap;

public Frm_Main()

{

InitializeComponent();

}

private void buttonOpen_Click(object sender, EventArgs e)

{

OpenFileDialog openFileDialog = new OpenFileDialog();

openFileDialog.Filter = "*.jpg|*.jpg";

openFileDialog.Title = "打开图像文件";

openFileDialog.Multiselect = false;

if (openFileDialog.ShowDialog() == DialogResult.OK)

{

if (bitmap != null)

{

bitmap.Dispose();

}

string fileName = openFileDialog.FileName;

bitmap = new Bitmap(fileName);

if (bitmap.Width > bitmap.Height)

{

pictureBox.Width = panel2.Width;

pictureBox.Height = (int)((double)bitmap.Height * panel2.Width / bitmap.Width);

}

else

{

pictureBox.Height = panel2.Height;

pictureBox.Width = (int)((double)bitmap.Width * panel2.Height / bitmap.Height);

}

pictureBox.Image = bitmap;

FileInfo f = new FileInfo(fileName);

this.Text = "图像转换:" + f.Name;

this.label1.Text = f.Name;

buttonConvert.Enabled = true;

}

}

private void panel2_Resize(object sender, EventArgs e)

{

pictureBox.Top = panel1.Top;

pictureBox.Left = panel1.Left;

if (bitmap != null)

{

if (bitmap.Width > bitmap.Height)

{

pictureBox.Width = panel2.Width;

pictureBox.Height = (int)((double)bitmap.Height * panel2.Width / bitmap.Width);

}

else

{

pictureBox.Height = panel2.Height;

pictureBox.Width = (int)((double)bitmap.Width * panel2.Height / bitmap.Height);

}

}

else

{

pictureBox.Width = panel2.Width;

pictureBox.Height = panel2.Height;

}

pictureBox.Refresh();

}

private void buttonConvert_Click(object sender, EventArgs e)

{

if (comboBox.SelectedItem == null)//如果没有选定项

{

return;//退出本次操作

}

else

{

SaveFileDialog saveFileDialog = new SaveFileDialog();//实例化SaveFileDialog类

saveFileDialog.Title = "转化为:";//设置“另存为”对话框的题标

saveFileDialog.OverwritePrompt = true;//如果文件名存在则提示

saveFileDialog.CheckPathExists = true; //如果文件的路径不存在则提示

saveFileDialog.Filter = comboBox.Text + "|" + comboBox.Text;//设置文件类型

if (saveFileDialog.ShowDialog() == DialogResult.OK)//打开“另存为”对话框

{

string fileName = saveFileDialog.FileName;//获取另存为文件的路径及名称

bitmap.Save(fileName, ImageFormat.Bmp); //调用Save方法将图片保存为Bmp格式

FileInfo f = new FileInfo(fileName);//实例化FileInfo类

this.Text = "图像转换:" + f.Name;//设置窗体标题栏

label1.Text = f.Name;

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值