示例#1
0
        public override void SaveTo(string filename, ImageFileFormat format)
        {
            Direct3D.Surface surf = mTexture.Value.GetSurfaceLevel(0);
            bool             disposeSurfWhenDone = false;

            if (surf.Description.Pool == Pool.Default)
            {
                surf = CopyRenderTargetSurfaceToSysmem(surf);

                disposeSurfWhenDone = true;
            }

            //Direct3D.ImageFileFormat d3dformat = SlimDX.Direct3D9.ImageFileFormat.Png;

            //switch (format)
            //{
            //    case ImageFileFormat.Bmp: d3dformat = Direct3D.ImageFileFormat.Bmp; break;
            //    case ImageFileFormat.Png: d3dformat = Direct3D.ImageFileFormat.Png; break;
            //    case ImageFileFormat.Jpg: d3dformat = Direct3D.ImageFileFormat.Jpg; break;
            //    case ImageFileFormat.Tga: d3dformat = Direct3D.ImageFileFormat.Tga; break;
            //}

            DataRectangle rect = surf.LockRectangle(LockFlags.ReadOnly);

            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(surf.Description.Width, surf.Description.Height);
            var target = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
                                      System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                      System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            byte[] buffer = new byte[rect.Data.Length];
            rect.Data.Read(buffer, 0, (int)rect.Data.Length);

            Marshal.Copy(buffer, 0, target.Scan0, buffer.Length);

            bmp.UnlockBits(target);
            surf.UnlockRectangle();

            System.Drawing.Imaging.ImageFormat bmpFormat = System.Drawing.Imaging.ImageFormat.Png;

            switch (format)
            {
            case ImageFileFormat.Bmp: bmpFormat = System.Drawing.Imaging.ImageFormat.Bmp; break;

            case ImageFileFormat.Jpg: bmpFormat = System.Drawing.Imaging.ImageFormat.Jpeg; break;
            }

            bmp.Save(filename, bmpFormat);

            if (disposeSurfWhenDone)
            {
                surf.Dispose();
            }
        }
示例#2
0
        private static Drawing.Image GetThumbnail(Drawing.Image b, int destHeight, int destWidth)
        {
            System.Drawing.Image imgSource = b;
            System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
            int sW = 0, sH = 0;

            int sWidth = imgSource.Width;
            int sHeight = imgSource.Height;

            if (sHeight > destHeight || sWidth > destWidth)
            {
                if ((sWidth * destHeight) > (sHeight * destWidth))
                {
                    sW = destWidth;
                    sH = (destWidth * sHeight) / sWidth;
                }
                else
                {
                    sH = destHeight;
                    sW = (sWidth * destHeight) / sHeight;
                }
            }
            else
            {
                sW = sWidth;
                sH = sHeight;
            }
            Bitmap   outBmp = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage(outBmp);

            g.Clear(Color.Transparent);

            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode      = SmoothingMode.HighQuality;
            g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
            g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
            g.Dispose();

            EncoderParameters encoderParams = new EncoderParameters();

            long[] quality = new long[1];
            quality[0] = 100;
            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

            encoderParams.Param[0] = encoderParam;
            imgSource.Dispose();
            return(outBmp);
        }