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("反转成功!"); } }
指定压缩图片,可是本人使用了以后,最后生成的图片并没有按照本人指定大小去生成,反而跟之前的图片大小完全相同,反倒他在本人软件界面上生成了一个图(本人猜测就是那个缩略图)。感觉Graphics 这个操作完全没起到作用。
解决方案
5
这2句代码,分别创建了2个 Image 实例,一个是指定文件的图片,一个是目标图片
Bitmap b = new Bitmap(fileName);
Bitmap tempb = new Bitmap(width, height);
但为什么最后保存时却使用了 b.Save(…) 呢?这不就保存了 b 的副本了吗?
应该使用 tempb.Save 才对。
但是这句代码,是利用 Form 创建的 Graphics 对象
全部绘制操作都是在针对窗体
Graphics g = this.CreateGraphics();
所以,第29行的代码
g.DrawImage(b, new Rectangle(10, 10, 120, 120), // source rectangle
new Rectangle(0, 0, width, height), // destination rectangle
GraphicsUnit.Pixel);
是绘制 Form 的,不是绘制 tempb 的。
Bitmap b = new Bitmap(fileName);
Bitmap tempb = new Bitmap(width, height);
但为什么最后保存时却使用了 b.Save(…) 呢?这不就保存了 b 的副本了吗?
应该使用 tempb.Save 才对。
但是这句代码,是利用 Form 创建的 Graphics 对象
全部绘制操作都是在针对窗体
Graphics g = this.CreateGraphics();
所以,第29行的代码
g.DrawImage(b, new Rectangle(10, 10, 120, 120), // source rectangle
new Rectangle(0, 0, width, height), // destination rectangle
GraphicsUnit.Pixel);
是绘制 Form 的,不是绘制 tempb 的。
70
Bitmap 本身就提供有图片缩放功能,并不需要借助其他工具
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 中有一个缩小的(120×120)的原图图样
5
Bitmap b = new Bitmap(fileName);
int width = b.Width;
int height = b.Height;
int width = b.Width;
int height = b.Height;
Bitmap tempb = new Bitmap(width, height);
两张图片确实是一样大小啊
而且也看不明斑你想干啥
15
这个看起来很明白啊。
5
System.Drawing.Bitmap b = null;
b.GetThumbnailImage() 这是获取缩略图的。不过已经加载到内存了。效率不咋地。假如不考虑效率的情况下可以使用。
b.GetThumbnailImage() 这是获取缩略图的。不过已经加载到内存了。效率不咋地。假如不考虑效率的情况下可以使用。