c#中如何调用API函数GdipSaveImageToStream ?

解决方案 »

  1.   

    Image和Stream有很好的转换啊,为什么一定要用API呢?
      

  2.   

    是啊,API中没这个函数,这是哪里的函数啊
      

  3.   

    不过可以用如下的声明来调用这个API:
    [DllImport("gdiplus.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
    internal static extern int GdipSaveImageToStream(IntPtr image, UnsafeNativeMethods.IStream stream, ref Guid classId, IntPtr encoderParams);
      

  4.   

    我是想把从扫描仪出来的图像存到流中,然后直接在pictureBox中显示出来
    誰能帮帮忙?
      

  5.   

    就有这个构造函数啊:
    Bitmap bit = new Bitmap(stream);
      

  6.   

    //
    // 摘要:
    //     从指定的数据流初始化 System.Drawing.Bitmap 类的新实例。
    //
    // 参数:
    //   stream:
    //     用于加载图像的数据流。
    public Bitmap(Stream stream);
      

  7.   

    hbxtlhx(平民百姓) 
    不过可以用如下的声明来调用这个API:
    [DllImport("gdiplus.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
    internal static extern int GdipSaveImageToStream(IntPtr image, UnsafeNativeMethods.IStream stream, ref Guid classId, IntPtr encoderParams);------------------------
    能给个具体调用的过程吗?以前没有调用过API函数
      

  8.   

    从扫描仪得到的是IntPtr image的数据,怎样才能转为pictureBox可以显示的图像?
      

  9.   

    我是想把IntPtr image存到流中,然后再用Image.FromStream把它显示到pictureBox中,行得通吗?
      

  10.   

    lz:你这个函数是第三方的SDK吧,如果是,hbxtlhx的方法应该可以实现你的要求
      

  11.   

    觉得你最好用Image或Bitmap来和Stream交互,最好不用API
      

  12.   

    下面的代码是将扫描得到的数据保存到文件和:public class Gdip
        {
            private static ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();        private static bool GetCodecClsid(string filename, out Guid clsid)
            {
                clsid = Guid.Empty;
                string ext = Path.GetExtension(filename);
                if (ext == null)
                    return false;
                ext = "*" + ext.ToUpper();
                foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.FilenameExtension.IndexOf(ext) >= 0)
                    {
                        clsid = codec.Clsid;
                        return true;
                    }
                }
                return false;
            }public static bool SaveDIBAs(string picname, IntPtr bminfo, IntPtr pixdat)
            {
                SaveFileDialog sd = new SaveFileDialog();            sd.FileName = picname;
                sd.Title = "Save bitmap as...";
                sd.Filter = "Bitmap file (*.bmp)|*.bmp|TIFF file (*.tif)|*.tif|JPEG file (*.jpg)|*.jpg|PNG file (*.png)|*.png|GIF file (*.gif)|*.gif|All files (*.*)|*.*";
                sd.FilterIndex = 1;
                if (sd.ShowDialog() != DialogResult.OK)
                    return false;            Guid clsid;
                if (!GetCodecClsid(sd.FileName, out clsid))
                {
                    MessageBox.Show("Unknown picture format for extension " + Path.GetExtension(sd.FileName),
                                    "Image Codec", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }            IntPtr img = IntPtr.Zero;
                int st = GdipCreateBitmapFromGdiDib(bminfo, pixdat, ref img);
                if ((st != 0) || (img == IntPtr.Zero))
                    return false;            st = GdipSaveImageToFile(img, sd.FileName, ref clsid, IntPtr.Zero);
                GdipDisposeImage(img);
                return st == 0;
            }[DllImport("gdiplus.dll", ExactSpelling = true)]
            internal static extern int GdipCreateBitmapFromGdiDib(IntPtr bminfo, IntPtr      pixdat, ref IntPtr image);[DllImport("gdiplus.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
            internal static extern int GdipSaveImageToFile(IntPtr image, string           filename, [In] ref Guid clsid, IntPtr encparams);[DllImport("gdiplus.dll", ExactSpelling = true)]
            internal static extern int GdipDisposeImage(IntPtr image);
    }我想把它改成将扫描得到的数据直接显示在pictureBox中,如何做?