Пример #1
0
        public static byte[] WicResize(IWICComponentFactory factory, IWICBitmapFrameDecode frame, int width,
                                       int height, int quality)
        {
            // Prepare output stream to cache file
            var outputStream = new MemoryIStream();
            // Prepare PNG encoder
            var encoder = factory.CreateEncoder(Consts.GUID_ContainerFormatJpeg, null);

            encoder.Initialize(outputStream, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache);
            // Prepare output frame
            IWICBitmapFrameEncode outputFrame;
            var arg = new IPropertyBag2[1];

            encoder.CreateNewFrame(out outputFrame, arg);
            var propBag           = arg[0];
            var propertyBagOption = new PROPBAG2[1];

            propertyBagOption[0].pstrName = "ImageQuality";
            propBag.Write(1, propertyBagOption, new object[] { ((float)quality) / 100 });
            outputFrame.Initialize(propBag);
            double dpiX, dpiY;

            frame.GetResolution(out dpiX, out dpiY);
            outputFrame.SetResolution(dpiX, dpiY);

            uint ow, oh, w, h;

            frame.GetSize(out ow, out oh);
            if (ow > oh)
            {
                w = (uint)width;
                h = (uint)((double)height * (double)oh / (double)ow);
            }
            else
            {
                w = (uint)((double)height * (double)ow / (double)oh);
                h = (uint)height;
            }


            outputFrame.SetSize(w, h);
            // Prepare scaler
            var scaler = factory.CreateBitmapScaler();

            scaler.Initialize(frame, w, h, WICBitmapInterpolationMode.WICBitmapInterpolationModeFant);
            // Write the scaled source to the output frame
            outputFrame.WriteSource(scaler, new WICRect {
                X = 0, Y = 0, Width = (int)width, Height = (int)height
            });
            outputFrame.Commit();
            encoder.Commit();
            var outputArray = outputStream.ToArray();

            outputStream.Close();
            Marshal.ReleaseComObject(outputFrame);
            Marshal.ReleaseComObject(scaler);
            Marshal.ReleaseComObject(propBag);
            Marshal.ReleaseComObject(encoder);
            return(outputArray);
        }
Пример #2
0
        protected virtual RequestedAction Encode(IWICComponentFactory factory, IWICBitmapSource data, Size imageSize, ImageJob job)
        {
            WicEncoderPlugin encoder = new WicEncoderPlugin(job.Settings, job.SourcePathData);

            //Create the IStream/MemoryStream
            var outputStream = new MemoryIStream();

            job.ResultInfo["final.width"]  = imageSize.Width;
            job.ResultInfo["final.height"] = imageSize.Height;

            encoder.EncodeToStream(factory, data, imageSize, outputStream);

            object dest = job.Dest;

            // Try to save the bitmap
            if (dest is string)
            {
                //Make physical and resolve variable references all at the same time.
                job.FinalPath = job.ResolveTemplatedPath(job.Dest as string,
                                                         delegate(string var) {
                    if ("ext".Equals(var, StringComparison.OrdinalIgnoreCase))
                    {
                        return(encoder.Extension);
                    }
                    if ("width".Equals(var, StringComparison.OrdinalIgnoreCase))
                    {
                        return(imageSize.Width.ToString());
                    }
                    if ("height".Equals(var, StringComparison.OrdinalIgnoreCase))
                    {
                        return(imageSize.Height.ToString());
                    }
                    return(null);
                });
                //If requested, auto-create the parent directory(ies)
                if (job.CreateParentDirectory)
                {
                    string dirName = Path.GetDirectoryName(job.FinalPath);
                    if (!Directory.Exists(dirName))
                    {
                        Directory.CreateDirectory(dirName);
                    }
                }

                using (FileStream fs = new FileStream(job.FinalPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)) {
                    outputStream.WriteTo(fs);
                }
            }
            else if (dest is Stream)
            {
                outputStream.WriteTo((Stream)dest);
            }
            else
            {
                return(RequestedAction.None);
            }

            return(RequestedAction.Cancel);
        }
Пример #3
0
        public void DecodeResizeAndEncode(byte[] fileBytes, long lData, uint width, uint height, Stream writeTo)
        {
            //A list of COM objects to destroy
            List <object> com = new List <object>();

            try {
                //Create the factory
                IWICComponentFactory factory = (IWICComponentFactory) new WICImagingFactory();
                com.Add(factory);

                //Wrap the byte[] with a IWICStream instance
                var streamWrapper = factory.CreateStream();
                streamWrapper.InitializeFromMemory(fileBytes, (uint)lData);
                com.Add(streamWrapper);

                var decoder = factory.CreateDecoderFromStream(streamWrapper, null,
                                                              WICDecodeOptions.WICDecodeMetadataCacheOnLoad);
                com.Add(decoder);

                IWICBitmapFrameDecode frame = decoder.GetFrame(0);
                com.Add(frame);


                var scaler = factory.CreateBitmapScaler();
                scaler.Initialize(frame, width, height, WICBitmapInterpolationMode.WICBitmapInterpolationModeFant);
                com.Add(scaler);


                var outputStream = new MemoryIStream();


                EncodeToStream(factory, scaler, outputStream);

                outputStream.WriteTo(writeTo);
            } finally {
                //Manually cleanup all the com reference counts, aggressively
                while (com.Count > 0)
                {
                    Marshal.ReleaseComObject(com[com.Count - 1]); //In reverse order, so no item is ever deleted out from under another.
                    com.RemoveAt(com.Count - 1);
                }
            }
        }
Пример #4
0
        public void Write(System.Drawing.Image i, System.IO.Stream s)
        {
            //A list of COM objects to destroy
            List <object> com = new List <object>();

            try {
                var factory = (IWICComponentFactory) new WICImagingFactory();
                com.Add(factory);


                Stopwatch conversion = new Stopwatch();
                conversion.Start();

                IWICBitmap b = ConversionUtils.ToWic(factory, i as Bitmap);

                conversion.Stop();

                Stopwatch encoding = new Stopwatch();
                encoding.Start();

                //Prepare output stream
                var outputStream = new MemoryIStream();

                EncodeToStream(factory, b, i.Size, outputStream);
                encoding.Stop();

                Stopwatch streaming = new Stopwatch();
                streaming.Start();
                outputStream.WriteTo(s);
                streaming.Stop();
            } finally {
                //Manually cleanup all the com reference counts, aggressively
                while (com.Count > 0)
                {
                    Marshal.ReleaseComObject(com[com.Count - 1]); //In reverse order, so no item is ever deleted out from under another.
                    com.RemoveAt(com.Count - 1);
                }
            }
        }
Пример #5
0
        public static byte[] WicResize(IWICComponentFactory factory, IWICBitmapFrameDecode frame, int width,
            int height, int quality)
        {
            // Prepare output stream to cache file
            var outputStream = new MemoryIStream();
            // Prepare PNG encoder
            var encoder = factory.CreateEncoder(Consts.GUID_ContainerFormatJpeg, null);
            encoder.Initialize(outputStream, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache);
            // Prepare output frame
            IWICBitmapFrameEncode outputFrame;
            var arg = new IPropertyBag2[1];
            encoder.CreateNewFrame(out outputFrame, arg);
            var propBag = arg[0];
            var propertyBagOption = new PROPBAG2[1];
            propertyBagOption[0].pstrName = "ImageQuality";
            propBag.Write(1, propertyBagOption, new object[] {((float) quality)/100});
            outputFrame.Initialize(propBag);
            double dpiX, dpiY;
            frame.GetResolution(out dpiX, out dpiY);
            outputFrame.SetResolution(dpiX, dpiY);

            uint ow, oh, w, h;
            frame.GetSize(out ow, out oh);
            if (ow > oh ) {
                w = (uint)width;
                h = (uint)((double)height * (double)oh / (double)ow);
            } else {
                w = (uint)((double)height * (double)ow / (double)oh);
                h = (uint)height;
            }

            outputFrame.SetSize(w, h);
            // Prepare scaler
            var scaler = factory.CreateBitmapScaler();
            scaler.Initialize(frame, w, h, WICBitmapInterpolationMode.WICBitmapInterpolationModeFant);
            // Write the scaled source to the output frame
            outputFrame.WriteSource(scaler, new WICRect {X = 0, Y = 0, Width = (int) width, Height = (int) height});
            outputFrame.Commit();
            encoder.Commit();
            var outputArray = outputStream.ToArray();
            outputStream.Close();
            Marshal.ReleaseComObject(outputFrame);
            Marshal.ReleaseComObject(scaler);
            Marshal.ReleaseComObject(propBag);
            Marshal.ReleaseComObject(encoder);
            return outputArray;
        }