我希望将一个图片(比如JPG、GIF、PNG、BMP)这几种类型的图片进行格式互转,另外在转换过程中希望能压缩图片的大小以及色素等。有没有一个比较方便的现成的类可以使用,看了SYSTEM。DRAWING里的类,好复杂,其中的数据结构都看不太懂。

解决方案 »

  1.   

    提供一个方案:
      你可以先创建一个BitMap,如:
      Bitmap bmp = new Bitmap(宽, 高,色素[PixelFormat枚举]);
      然后使用Graphics g = Graphics.FromImage(bmp);
      使用g.DrawImage();g.Save();把图像绘制出来;
      最后用bmp.Save(文件名,图片格式[ImageFormat枚举]);
      就可以实现图片的转换.
      

  2.   

    tks
    any other detail to help me ?
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {            
                Bitmap bmp = new Bitmap(320, 240, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);//用宽/高/色度创建Bitmap对象.
                Graphics g = Graphics.FromImage(bmp);
                g.DrawImage(new Bitmap("源文件的位置"), new Point(0, 0));
                g.Save();
                bmp.Save("新保存文件的位置", System.Drawing.Imaging.ImageFormat.Bmp);//指定新图片保存位置和格式.        }
        }
    }
      

  4.   

    非常感谢。
    就以上代码问几个问题,不好意思有点懒
    1、  Graphics g = Graphics.FromImage(bmp);
         g.DrawImage(new Bitmap("源文件的位置"), new Point(0, 0));
    这样是不是只能从BMP格式的图片转成其他的图片,这个源格式是取决于DRAWIMGAGE方法时读进去的图片格式,还是取决与BITMAP构造函数?不好意思。
    2、另外如果图片是从WEB上传上来,我读到该图片时还没存放到硬盘上时,这个图片能否直接传递给BITMAP对象?
    3、如果上传上来的图片后缀名和实际的格式不对时,我有没有办法不根据后缀名去判断其真实的图片格式?非常感谢您的帮助。
      

  5.   

    1.不是只能处理BMP.Bitmap类能把其他格式图像转换成位图.
    2.如果是WEB上传文件可以考虑使用Bitmap(Stream stream)这个重载.
    3.没试过.
      

  6.   

    哈哈,OKOK,非常感谢,有这个够了。我的类也写了一半了,