示例#1
0
 public void Free()
 {
     if (!IsNull)
     {
         GDImport.gdFree(mPtr);
         mPtr = IntPtr.Zero;
     }
 }
示例#2
0
文件: GD.cs 项目: ststeiger/GdCaptcha
        public bool Save(FileType type, Stream outstream, int parameter)
        {
            try
            {
                IntPtr imgDataHandle = IntPtr.Zero;
                int    len;
                try
                {
                    switch (type)
                    {
                    case FileType.Jpeg:
                        imgDataHandle = GDImport.gdImageJpegPtr(this.Handle, out len, parameter);
                        break;

                    case FileType.Png:
                        imgDataHandle = GDImport.gdImagePngPtr(this.Handle, out len);
                        break;

                    case FileType.Gd:
                        imgDataHandle = GDImport.gdImageGdPtr(this.Handle, out len);
                        break;

                    case FileType.Gd2:
                        imgDataHandle = GDImport.gdImageGd2Ptr(this.Handle, 0, GD2_FMT_COMPRESSED, out len);
                        break;

                    case FileType.WBMP:
                        imgDataHandle = GDImport.gdImageGdPtr(this.Handle, out len);
                        break;

                    case FileType.Xbm:
                    case FileType.Xpm:
                        throw new ApplicationException(type + " not implemented.");

                    default:
                        throw new ApplicationException(type + " is an unknown file type.");
                    }

                    if (imgDataHandle == IntPtr.Zero)
                    {
                        throw new ApplicationException("function Save(FileType type, Stream outstream, int parameter )");
                    }

                    byte[] imgData = new byte[len];

                    Marshal.Copy(imgDataHandle, imgData, 0, len);
                    MemoryStream img = new MemoryStream(imgData);

                    int    BUFFER = 4096;
                    byte[] buffer = new byte[BUFFER];
                    int    numBytes;

                    while ((numBytes = img.Read(buffer, 0, BUFFER)) > 0)
                    {
                        outstream.Write(imgData, 0, len);
                    }
                }
                finally
                {
                    // 2004-01-10 - Must free memory!! - Kevin Tam
                    if (imgDataHandle != IntPtr.Zero)
                    {
                        GDImport.gdFree(imgDataHandle);
                    }
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException("function Save(FileType type, Stream outstream, int parameter ) failed", e);
            }
            return(true);
        }