سؤال

I'm a .net developer and I'm using WindowsAPICodePackage WIC to render images. I want to convert WIC ImagingBitmap to a gdi+ bitmap or to a MemoryStream instead using its own method "SaveToFile". Is there a way? Here is my code, please help:

int width = 256;
int height = 256;

D2DFactory d2DFactory = D2DFactory.CreateFactory(D2DFactoryType.SingleThreaded);
RenderTargetProperties renderTargetProperties = new RenderTargetProperties
{
    PixelFormat = new PixelFormat(Format.B8G8R8A8UNorm, AlphaMode.Premultiplied),
    Usage = RenderTargetUsages.GdiCompatible,
    RenderTargetType = RenderTargetType.Software
};

ImagingFactory wicFactory = ImagingFactory.Create();
ImagingBitmap imagingBitmap = wicFactory.CreateImagingBitmap((uint)width
    , (uint)height, Microsoft.WindowsAPICodePack.DirectX.WindowsImagingComponent.PixelFormats.Pbgra32Bpp, BitmapCreateCacheOption.CacheOnLoad);

RenderTarget renderTarget = d2DFactory.CreateWicBitmapRenderTarget(imagingBitmap, renderTargetProperties);
renderTarget.BeginDraw();

Random r = new Random();
for (int i = 0; i < 10; i++)
{
    renderTarget.DrawLine(new Point2F(r.Next(0, width), r.Next(0, height)), new Point2F(r.Next(0, width), r.Next(0, height))
        , renderTarget.CreateSolidColorBrush(new ColorF(1, 1, 0)), 4);
}
renderTarget.EndDraw();

string imageFilePath = @"c:\wic.png";
imagingBitmap.SaveToFile(wicFactory, ContainerFormats.Png, imageFilePath);

Thanks,

هل كانت مفيدة؟

المحلول

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top