110,538
社区成员
发帖
与我相关
我的任务
分享
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.InitialDirectory = "C://";
fileDialog.Filter = "txt files (*.jpg)|*.jpg|All files (*.*)|*.*";
fileDialog.FilterIndex = 1;
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string tempfilename=@"C:\yuantu\a\temp1001.jpg";
this.textBox1.Text = fileDialog.FileName;
string fileName = fileDialog.FileName; ;
//对各个文件进行操作
Graphics g = this.CreateGraphics();
Bitmap b = new Bitmap(fileName);
int width = b.Width;
int height = b.Height;
Bitmap tempb = new Bitmap(width, height);
// 改变图像大小使用低质量的模式
g.FillRectangle(Brushes.White, new Rectangle(0, 0, width, height));
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
g.DrawImage(b, new Rectangle(10, 10, 120, 120), // source rectangle
new Rectangle(0, 0, width, height), // destination rectangle
GraphicsUnit.Pixel);
g.Dispose();
b.Save(tempfilename);
//Bitmap c = MyLog.RevPic( MyLog.RevPic(b));
//c.Save(fileName);
MessageBox.Show("反转成功!");
}
}
Bitmap src = new Bitmap("源文件名");
int width = src.Width / 2; //计算目标图的宽度,比如一半
int height = src.Height / 2; //计算目标图的高度,比如一半
Bitmap des = new Bitmap(src, width, height);
dec.Save("目标文件名");
当然,作为学习也是应该尝试使用其他方法的(不去尝试也就不可能有创新)
你有
Bitmap b = new Bitmap(fileName); //原图
int width = b.Width;
int height = b.Height;
Bitmap tempb = new Bitmap(width, height); //目标图
绘制应在 tempb 中进行,所以
Graphics g = this.CreateGraphics();
应改为
Graphics g = Graphics.FromImage(tempb);
因为 this.CreateGraphics() 是当前 form 的绘图设备,结果自然会反映到 form 中
保存时应是
tempb.Save(tempfilename);
但需要注意的是:
1、tempb 和 b 的尺寸是一样大的
2、tempb 中有一个缩小的(120x120)的原图图样
private void button1_Click(object sender, EventArgs e)
{
var cdlg = new OpenFileDialog();
// 初始目录: 我的图片文件夹
cdlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
cdlg.Filter = "图片文件|*.bmp;*.jpg;*.png";
cdlg.Title = "打开";
if (cdlg.ShowDialog(this) == DialogResult.Cancel) // 取消则返回
return;
var path = cdlg.FileName;
var dir = System.IO.Path.GetDirectoryName(path); // 获取文件目录
var fn = System.IO.Path.GetFileNameWithoutExtension(path); // 获取文件名 不带扩展名
var ext = System.IO.Path.GetExtension(path); // 获取扩展名 带.
// 新文件与源文件位于同一目录,文件名后追加 "_crop"
path = System.IO.Path.Combine(dir, string.Format("{0}_crop{1}", fn, ext));
textBox1.Text = cdlg.FileName; // 源文件名
textBox2.Text = path; // 目标文件名
try
{
using (var srcImage = Image.FromFile(cdlg.FileName)) // 创建源文件 Image 实例
{
using (var desImage = new Bitmap(srcImage.Width, srcImage.Height)) // 创建目标 Image 实例
{
using (var g = Graphics.FromImage(desImage)) // 通过目标 Image 创建 Graphics 对象
{
var srcRect = new Rectangle(0, 0, srcImage.Width - 1, srcImage.Height - 1); // 源图区域 Rectangle
var desRect = new Rectangle(
(desImage.Width - desImage.Width / 2) / 2,
(desImage.Height - desImage.Height / 2) / 2,
desImage.Width / 2,
desImage.Height / 2); // 目标区域 Rectangle
g.FillRectangle(Brushes.White, 0, 0, desImage.Width - 1, desImage.Height - 1); // 填充背景色
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low; // 这个以前还怎没怎么用过
g.DrawImage(srcImage, desRect, srcRect, GraphicsUnit.Pixel); // 将源图区域绘制到目标区域
desImage.Save(path, srcImage.RawFormat); // 以源图格式保存图片
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(this,
string.Format("{0} 发生错误: {1}", ex.Source, ex.Message), "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}