C#关于Graphics的DrawImage提取图像部分的用法

zc1989621machao 2016-07-02 10:28:39

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 这个操作完全没起到作用。
...全文
2891 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 楼主 zc1989621machao 的回复:
反而跟之前的图片大小完全相同,反倒他在我软件界面上生成了一个图(我猜测就是那个缩略图)。感觉Graphics 这个操作完全没起到作用。
这个看起来很明白啊。
Poopaye 2016-07-03
  • 打赏
  • 举报
回复
Bitmap b = new Bitmap(fileName); int width = b.Width; int height = b.Height; Bitmap tempb = new Bitmap(width, height); 两张图片确实是一样大小啊 而且也看不明斑你想干啥
xuzuning 2016-07-03
  • 打赏
  • 举报
回复
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 中有一个缩小的(120x120)的原图图样
足球中国 2016-07-03
  • 打赏
  • 举报
回复
System.Drawing.Bitmap b = null; b.GetThumbnailImage() 这是获取缩略图的。不过已经加载到内存了。效率不咋地。如果不考虑效率的情况下可以使用。
龍过鸡年 2016-07-02
  • 打赏
  • 举报
回复
这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 的。
龍过鸡年 2016-07-02
  • 打赏
  • 举报
回复

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);
    }
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧