如何将位图图像另存为JPEG [英] How to save a bitmap image as JPEG

查看:117
本文介绍了如何将位图图像另存为JPEG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是文件未另存为JPEG.只是一个普通文件.

The problem is that the file is not saving as JPEG. Just a normal file.

到目前为止,这是我的代码:

This is my code so far:

private void btnSave_Click(object sender, EventArgs e)
{
    saveDialog.FileName = txtModelName.Text;

    if (saveDialog.ShowDialog() == DialogResult.OK)
    {
        Bitmap bmp = new Bitmap(pnlDraw.Width, pnlDraw.Height);

        pnlDraw.DrawToBitmap(bmp, new Rectangle(0, 0,
            pnlDraw.Width, pnlDraw.Height));

        bmp.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

推荐答案

如何在保存文件前检查文件名是否具有.jpg扩展名?

How about checking if file name has .jpg extension before saving it?

您还可以将saveDialog更改为仅允许用户选择.jpg图像.

You can also change saveDialog to only allow user selecting .jpg images.

private void btnSave_Click(object sender, EventArgs e)
{
    saveDialog.FileName = txtModelName.Text;
    saveDialog.DefaultExt = "jpg";
    saveDialog.Filter = "JPG images (*.jpg)|*.jpg";    

    if (saveDialog.ShowDialog() == DialogResult.OK)
    {
        Bitmap bmp = new Bitmap(pnlDraw.Width, pnlDraw.Height);

        pnlDraw.DrawToBitmap(bmp, new Rectangle(0, 0,
            pnlDraw.Width, pnlDraw.Height));

        var fileName = saveDialog.FileName;
        if(!System.IO.Path.HasExtension(fileName) || System.IO.Path.GetExtension(fileName) != "jpg")
            fileName = fileName + ".jpg";

        bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

这篇关于如何将位图图像另存为JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆