コード例 #1
0
        private bool CheckConvertion(MainForm form, IWICFormatConverterInfo info, Guid from, Guid to)
        {
            if (from == to)
            {
                return(true);
            }

            IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory();
            IWICPalette        palette = factory.CreatePalette();

            palette.InitializePredefined(WICBitmapPaletteType.WICBitmapPaletteTypeFixedBW, false);
            IWICBitmap          bitmap    = null;
            IWICFormatConverter converter = null;

            try
            {
                try
                {
                    converter = info.CreateInstance();
                }
                catch (Exception e)
                {
                    form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e));

                    return(false);
                }

                try
                {
                    bitmap = factory.CreateBitmap(1, 1, from, WICBitmapCreateCacheOption.WICBitmapCacheOnLoad);
                    bitmap.SetPalette(palette);
                }
                catch (Exception e)
                {
                    form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e), new DataEntry(Resources.PixelFormat, from));

                    return(false);
                }

                try
                {
                    converter.Initialize(bitmap, to, WICBitmapDitherType.WICBitmapDitherTypeNone, palette, 0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom);
                }
                catch (Exception e)
                {
                    form.CheckHRESULT(this, WinCodecError.WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT, e, new DataEntry(Resources.Source, from), new DataEntry(Resources.Destination, to));

                    return(false);
                }

                return(true);
            }
            finally
            {
                palette.ReleaseComObject();
                converter.ReleaseComObject();
                bitmap.ReleaseComObject();
                factory.ReleaseComObject();
            }
        }
コード例 #2
0
        public static void Main()
        {
            var wicFactory = new IWICImagingFactory();

            D2D1.D2D1CreateFactory(FactoryType.SingleThreaded, out ID2D1Factory d2dFactory);

            const string fileName = "output.jpg";
            const int    width    = 512;
            const int    height   = 512;

            var rectangleGeometry = d2dFactory.CreateRoundedRectangleGeometry(new RoundedRectangle(new RectangleF(128, 128, width - 128 * 2, height - 128 * 2), 32, 32));

            var wicBitmap = wicFactory.CreateBitmap(width, height, Vortice.DirectX.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad);

            var renderTargetProperties = new RenderTargetProperties(new Vortice.DirectX.Direct2D.PixelFormat(Format.Unknown, Vortice.DirectX.Direct2D.AlphaMode.Unknown));

            var d2dRenderTarget = d2dFactory.CreateWicBitmapRenderTarget(wicBitmap, renderTargetProperties);

            var solidColorBrush = d2dRenderTarget.CreateSolidColorBrush(new Color4(1.0f));

            d2dRenderTarget.BeginDraw();
            d2dRenderTarget.Clear(new Color4(0.0f, 0.0f, 0.0f, 1.0f));
            d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null);
            d2dRenderTarget.EndDraw();

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            using (var stream = wicFactory.CreateStream())
            {
                stream.Initialize(fileName, FileAccess.Write);

                // Initialize a Jpeg encoder with this stream
                using (var encoder = wicFactory.CreateEncoder(ContainerFormat.Jpeg))
                {
                    encoder.Initialize(stream);

                    // Create a Frame encoder
                    var props             = new SharpGen.Runtime.Win32.PropertyBag(IntPtr.Zero);
                    var bitmapFrameEncode = encoder.CreateNewFrame(props);
                    bitmapFrameEncode.Initialize(null);
                    bitmapFrameEncode.SetSize(width, height);
                    var pixelFormatGuid = Vortice.DirectX.WIC.PixelFormat.FormatDontCare;
                    bitmapFrameEncode.SetPixelFormat(ref pixelFormatGuid);
                    bitmapFrameEncode.WriteSource(wicBitmap, null);

                    bitmapFrameEncode.Commit();
                    encoder.Commit();
                }
            }

            using (var app = new TestApplication())
            {
                app.Run();
            }
        }
コード例 #3
0
        public static IComObject <IWICBitmap> CreateBitmap(this IWICImagingFactory factory, int width, int height, Guid pixelFormat, WICBitmapCreateCacheOption option = WICBitmapCreateCacheOption.WICBitmapNoCache)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            factory.CreateBitmap(width, height, pixelFormat, option, out var value).ThrowOnError();
            return(new ComObject <IWICBitmap>(value));
        }
コード例 #4
0
        protected override bool ProcessEncoder(MainForm form, IWICBitmapEncoder encoder, object tag)
        {
            IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory();
            IWICBitmap         bitmap  = factory.CreateBitmap(16, 16, Consts.GUID_WICPixelFormat32bppBGRA, WICBitmapCreateCacheOption.WICBitmapCacheOnLoad);
            IWICPalette        palette = factory.CreatePalette();

            palette.InitializePredefined(WICBitmapPaletteType.WICBitmapPaletteTypeFixedBW, false);
            IWICColorContext context = factory.CreateColorContext();

            context.InitializeFromExifColorSpace(ExifColorSpace.sRGB);

            ComponentInfoHelper.CheckEquals <IWICBitmapEncoderInfo>(form, encoder.GetEncoderInfo, this, Extensions.CompareInfos);
            Check(form, encoder.SetPalette, palette);
            Check(form, encoder.SetThumbnail, bitmap);
            Check(form, encoder.SetPreview, bitmap);
            try
            {
                encoder.SetColorContexts(1, new IWICColorContext[] { context });
            }
            catch (Exception e)
            {
                form.CheckHRESULT(this, WinCodecError.WINCODEC_ERR_UNSUPPORTEDOPERATION, e);
            }

            try
            {
                encoder.Commit();
                form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources._0_ShouldFail, "IWICBitmapEncoder::Commit(...)"), new DataEntry(Resources.FrameCount, 0));
            }
            catch (Exception e)
            {
                form.CheckHRESULT(this, WinCodecError.WINCODEC_ERR_FRAMEMISSING, e, new DataEntry(Resources.FrameCount, 0));
            }

            palette.ReleaseComObject();
            bitmap.ReleaseComObject();
            factory.ReleaseComObject();
            context.ReleaseComObject();

            return(base.ProcessEncoder(form, encoder, tag));
        }
コード例 #5
0
        protected override bool ProcessEncoder(MainForm form, IWICBitmapEncoder encoder, object tag)
        {
            IWICImagingFactory    factory = (IWICImagingFactory) new WICImagingFactory();
            IWICBitmap            bitmap  = factory.CreateBitmap(1, 1, Consts.GUID_WICPixelFormat128bpp7ChannelsAlpha, WICBitmapCreateCacheOption.WICBitmapCacheOnLoad);
            IWICBitmapFrameEncode frame   = null;

            try
            {
                try
                {
                    encoder.CreateNewFrame(out frame, null);
                }
                catch (Exception e)
                {
                    form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e));
                }

                if (frame != null)
                {
                    try
                    {
                        frame.Initialize(null);
                    }
                    catch (Exception e)
                    {
                        form.Add(this, e.TargetSite.ToString(Resources._0_Failed, "NULL"), new DataEntry(e));
                        frame.ReleaseComObject();
                        frame = null;
                    }
                }
                if (frame != null)
                {
                    try
                    {
                        frame.WriteSource(bitmap, null);
                    }
                    catch (Exception e)
                    {
                        form.Add(this, e.TargetSite.ToString(Resources._0_Failed, "..., NULL"), new DataEntry(e));
                        frame.ReleaseComObject();
                        frame = null;
                    }
                }

                if (frame != null)
                {
                    try
                    {
                        frame.Commit();
                        encoder.Commit();
                    }
                    catch (Exception e)
                    {
                        form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e));
                    }
                }
            }
            finally
            {
                frame.ReleaseComObject();
                bitmap.ReleaseComObject();
                factory.ReleaseComObject();
            }

            return(base.ProcessEncoder(form, encoder, tag));
        }
コード例 #6
0
 public static IWICBitmap CreateBitmap(this IWICImagingFactory imagingFactory, WICSize size, Guid pixelFormat, WICBitmapCreateCacheOption option)
 {
     return(imagingFactory.CreateBitmap(size.Width, size.Height, pixelFormat, option));
 }
コード例 #7
0
ファイル: Program.cs プロジェクト: phizch/Vortice.Windows
        public static void Main()
        {
            var wicFactory = new IWICImagingFactory();

            D2D1.D2D1CreateFactory(out ID2D1Factory d2dFactory);

            DWrite.DWriteCreateFactory(out IDWriteFactory dwriteFactory).CheckError();
            var textFormat = dwriteFactory.CreateTextFormat("Calibri", 20);

            const string fileName = "output.jpg";
            const int    width    = 512;
            const int    height   = 512;

            var rectangleGeometry = d2dFactory.CreateRoundedRectangleGeometry(new RoundedRectangle(new RectangleF(128, 128, width - 128 * 2, height - 128 * 2), 32, 32));

            var wicBitmap = wicFactory.CreateBitmap(width, height, Vortice.WIC.PixelFormat.Format32bppBGR, BitmapCreateCacheOption.CacheOnLoad);

            var renderTargetProperties = new RenderTargetProperties(Vortice.Direct2D1.PixelFormat.Unknown);

            var d2dRenderTarget = d2dFactory.CreateWicBitmapRenderTarget(wicBitmap, renderTargetProperties);

            var solidColorBrush    = d2dRenderTarget.CreateSolidColorBrush(new Color4(1.0f, 1.0f, 1.0f, 1.0f));
            var redSolidColorBrush = d2dRenderTarget.CreateSolidColorBrush(new Color4(1.0f, 0.0f, 0.0f, 1.0f));

            d2dRenderTarget.BeginDraw();
            d2dRenderTarget.Clear(new Color4(0.0f, 0.0f, 0.0f, 1.0f));
            d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null);
            d2dRenderTarget.DrawText("Hello", textFormat, new Rect(0, 0, 120, 24), redSolidColorBrush);
            d2dRenderTarget.EndDraw();

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            using (var stream = wicFactory.CreateStream(fileName, FileAccess.Write))
            {
                // Initialize a Jpeg encoder with this stream
                using (var encoder = wicFactory.CreateEncoder(ContainerFormat.Jpeg, stream))
                {
                    // Create a Frame encoder
                    var props             = new SharpGen.Runtime.Win32.PropertyBag();
                    var bitmapFrameEncode = encoder.CreateNewFrame(props);
                    bitmapFrameEncode.Initialize(null);
                    bitmapFrameEncode.SetSize(width, height);
                    bitmapFrameEncode.SetPixelFormat(Vortice.WIC.PixelFormat.FormatDontCare);
                    bitmapFrameEncode.WriteSource(wicBitmap);

                    bitmapFrameEncode.Commit();
                    encoder.Commit();
                }
            }

            /*using(var stream = File.OpenRead(fileName))
             * {
             *  GetTextureDimensions(wicFactory, stream);
             * }*/

            using (var app = new TestApplication())
            {
                app.Run();
            }
        }