public void CreateRenderTarget(SharpDX.WIC.Bitmap wicBitmap) { if (renderTarget != null) { return; } float dpiX = 96, dpiY = 96; using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero)) { dpiX = graphics.DpiX; dpiY = graphics.DpiY; } var renderTargetProperties = new RenderTargetProperties( RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Unknown), dpiX, dpiY, RenderTargetUsage.GdiCompatible, FeatureLevel.Level_DEFAULT); renderTarget = new WicRenderTarget(d2dFactory, wicBitmap, renderTargetProperties); IntPtr gdirtPtr; renderTarget.QueryInterface(Guid.Parse("e0db51c3-6f77-4bae-b3d5-e47509b35838"), out gdirtPtr); gdiRenderTarget = new GdiInteropRenderTarget(gdirtPtr); }
async Task SaveToFile(SharpDX.WIC.Bitmap bitmap) // debugging helper method, remove when no longer needed. { var width = bitmap.Size.Width; var height = bitmap.Size.Height; var pixels = new SharpDX.ColorBGRA[bitmap.Size.Width * bitmap.Size.Height]; bitmap.CopyPixels(pixels); var bytes = new byte[width * height * 4]; for (var i = 0; i < pixels.Length; ++i) { bytes[i * 4 + 0] = (byte)pixels[i].B; bytes[i * 4 + 1] = (byte)pixels[i].G; bytes[i * 4 + 2] = (byte)pixels[i].R; bytes[i * 4 + 3] = (byte)pixels[i].A; } var folder = ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync("test.png", CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.Size.Width, (uint)bitmap.Size.Height, 96, 96, bytes); await encoder.FlushAsync(); } }
public InfoText(SharpDX.Direct3D11.Device device, int width, int height) { _immediateContext = device.ImmediateContext; _rect.Size = new Size2F(width, height); _bitmapSize = width * height * 4; IsEnabled = true; using (var factoryWic = new SharpDX.WIC.ImagingFactory()) { _wicBitmap = new SharpDX.WIC.Bitmap(factoryWic, width, height, _pixelFormat, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); } var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), 0, 0, RenderTargetUsage.None, SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT); using (var factory2D = new SharpDX.Direct2D1.Factory()) { _wicRenderTarget = new WicRenderTarget(factory2D, _wicBitmap, renderTargetProperties); _wicRenderTarget.TextAntialiasMode = TextAntialiasMode.Default; } using (var factoryDWrite = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared)) { _textFormat = new TextFormat(factoryDWrite, "Tahoma", 20); } Color4 color = new Color4(1, 1, 1, 1); _sceneColorBrush = new SolidColorBrush(_wicRenderTarget, color); _clearColor = color; _clearColor.Alpha = 0; _renderTexture = new Texture2D(device, new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write, Format = Format.R8G8B8A8_UNorm, Height = height, Width = width, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Dynamic }); OverlayBufferView = new ShaderResourceView(device, _renderTexture, new ShaderResourceViewDescription() { Format = _renderTexture.Description.Format, Dimension = ShaderResourceViewDimension.Texture2D, Texture2D = new ShaderResourceViewDescription.Texture2DResource() { MipLevels = 1, MostDetailedMip = 0 } }); }
public void Dispose() { if (stream != null) { stream.Dispose(); stream = null; } if (d2dRenderTarget != null) { d2dRenderTarget.Dispose(); d2dRenderTarget = null; } if (wicBitmap != null) { wicBitmap.Dispose(); wicBitmap = null; } if (d2dFactory != null) { d2dFactory.Dispose(); d2dFactory = null; } if (wicFactory != null) { wicFactory.Dispose(); wicFactory = null; } if (memStream != null) { memStream.Dispose(); } }
public InfoText(SharpDX.Direct3D11.Device device, int width, int height) { _immediateContext = device.ImmediateContext; _rect.Size = new Size2F(width, height); _bitmapSize = width * height * 4; IsEnabled = true; using (var factoryWic = new SharpDX.WIC.ImagingFactory()) { _wicBitmap = new SharpDX.WIC.Bitmap(factoryWic, width, height, _pixelFormat, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); } var renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), 0, 0, RenderTargetUsage.None, SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT); using (var factory2D = new SharpDX.Direct2D1.Factory()) { _wicRenderTarget = new WicRenderTarget(factory2D, _wicBitmap, renderTargetProperties); _wicRenderTarget.TextAntialiasMode = TextAntialiasMode.Default; } using (var factoryDWrite = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared)) { _textFormat = new TextFormat(factoryDWrite, "Tahoma", 20); } Color4 color = new Color4(1, 1, 1, 1); _sceneColorBrush = new SolidColorBrush(_wicRenderTarget, color); _clearColor = color; _clearColor.Alpha = 0; _renderTexture = new Texture2D(device, new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write, Format = Format.R8G8B8A8_UNorm, Height = height, Width = width, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Dynamic }); OverlayBufferRes = new ShaderResourceView(device, _renderTexture, new ShaderResourceViewDescription() { Format = _renderTexture.Description.Format, Dimension = ShaderResourceViewDimension.Texture2D, Texture2D = new ShaderResourceViewDescription.Texture2DResource() { MipLevels = 1, MostDetailedMip = 0 } }); }
public D2DGraphics(SharpDX.WIC.Bitmap wicBitmap) { renderTargetMode = RenderTargetMode.WIC; CreateDeviceIndependentResource(); this.wicBitmap = wicBitmap; CreateDeviceDependentResource(); }
public static System.Drawing.Bitmap ToSD(this s.WIC.Bitmap bmp) { using (var bl = bmp.Lock(s.WIC.BitmapLockFlags.Read)) { var pixelFormat = bmp.PixelFormat.ToSDPixelFormat(); return(new System.Drawing.Bitmap(bmp.Size.Width, bmp.Size.Height, bl.Data.Pitch, pixelFormat, bl.Data.DataPointer)); } }
/// <summary> /// Function to create 2D Gorgon image data from a single System.Drawing.Image. /// </summary> /// <param name="wic">Windows Imaging Component interface to use.</param> /// <param name="image">An image to convert.</param> /// <param name="options">Options for conversion.</param> /// <returns>The converted image data.</returns> public static GorgonImageData Create2DImageDataFromImage(GorgonWICImage wic, Image image, GorgonGDIOptions options) { if (options.Format == BufferFormat.Unknown) { options.Format = GetBufferFormat(image.PixelFormat); } if (options.Format == BufferFormat.Unknown) { throw new GorgonException(GorgonResult.FormatNotSupported, string.Format(Resources.GORGFX_FORMAT_NOT_SUPPORTED, image.PixelFormat)); } if (options.Width < 1) { options.Width = image.Width; } if (options.Height < 1) { options.Height = image.Height; } // Specify 0 to generate a full mip chain. options.MipCount = options.MipCount < 1 ? GorgonImageData.GetMaxMipCount(options.Width, options.Height) : options.MipCount.Min(GorgonImageData.GetMaxMipCount(options.Width, options.Height)); // Create our settings. var settings = new GorgonTexture2DSettings { Width = options.Width, Height = options.Height, MipCount = options.MipCount, ArrayCount = 1, Format = options.Format, AllowUnorderedAccessViews = options.AllowUnorderedAccess, ShaderViewFormat = options.ViewFormat, Usage = options.Usage }; // Create our image data. var data = new GorgonImageData(settings); // Using the image, convert to a WIC bitmap object. using (Bitmap bitmap = wic.CreateWICImageFromImage(image)) { for (int mipLevel = 0; mipLevel < options.MipCount; mipLevel++) { var buffer = data.Buffers[mipLevel]; wic.AddWICBitmapToImageData(bitmap, options.Filter, options.Dither, buffer, options.UseClipping); } } return(data); }
//TODO: check this way to getting back texture //public BitmapSource ToBitmap() { // if (_d3D11Image == null) // return null; // // Copy back buffer to WriteableBitmap. // int width = _d3D11Image.PixelWidth; // int height = _d3D11Image.PixelHeight; // var format = EnableAlpha ? PixelFormats.Bgra32 : PixelFormats.Bgr32; // var writeableBitmap = new WriteableBitmap(width, height, 96, 96, format, null); // writeableBitmap.Lock(); // try { // uint[] data = new uint[width * height]; // _d3D11Image.TryGetData(data); // // Get a pointer to the back buffer. // unsafe { // uint* pBackbuffer = (uint*)writeableBitmap.BackBuffer; // for (int i = 0; i < data.Length; i++) // pBackbuffer[i] = data[i]; // } // writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height)); // } finally { // writeableBitmap.Unlock(); // } // return writeableBitmap; //} static void Copy(Texture2D texture, Stream stream, SharpDX.Direct3D11.Device device) { var desc = new Texture2DDescription { Width = (int)texture.Description.Width, Height = (int)texture.Description.Height, MipLevels = 1, ArraySize = 1, Format = texture.Description.Format, Usage = ResourceUsage.Staging, SampleDescription = new SampleDescription(1, 0), BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, OptionFlags = ResourceOptionFlags.None }; using (var factory = new SharpDX.WIC.ImagingFactory()) { using (var textureCopy = new Texture2D(device, desc)) { device.ImmediateContext.CopyResource(texture, textureCopy); var dataBox = device.ImmediateContext.MapSubresource( textureCopy, 0, 0, MapMode.Read, global::SharpDX.Direct3D11.MapFlags.None, out SharpDX.DataStream dataStream); using (dataStream) { var t = dataStream.ReadByte(); //ReadFloat(); var dataRectangle = new SharpDX.DataRectangle { DataPointer = dataStream.DataPointer, Pitch = dataBox.RowPitch }; //https://github.com/sharpdx/Toolkit/blob/master/Source/Toolkit/SharpDX.Toolkit.Graphics/WICHelper.cs using (var bitmap = new SharpDX.WIC.Bitmap(factory, textureCopy.Description.Width, textureCopy.Description.Height, SharpDX.WIC.PixelFormat.Format32bppRGBA, dataRectangle, 0)) { stream.Position = 0; using (var bitmapEncoder = new SharpDX.WIC.PngBitmapEncoder(factory, stream)) { using (var bitmapFrameEncode = new SharpDX.WIC.BitmapFrameEncode(bitmapEncoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height); var pixelFormat = SharpDX.WIC.PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormat); bitmapFrameEncode.WriteSource(bitmap); bitmapFrameEncode.Commit(); bitmapEncoder.Commit(); } } } device.ImmediateContext.UnmapSubresource(textureCopy, 0); } } } }
public DXGI2DContext(int width, int height) { this.width = width; this.height = height; wicFactory = new SharpDX.WIC.ImagingFactory(); d2dFactory = new SharpDX.Direct2D1.Factory(); wicBitmap = new SharpDX.WIC.Bitmap(wicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppBGR, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); renderTargetProperties = new RenderTargetProperties(RenderTargetType.Default, new PixelFormat(Format.Unknown, AlphaMode.Unknown), 0, 0, RenderTargetUsage.None, SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT); d2dRenderTarget = new WicRenderTarget(d2dFactory, wicBitmap, renderTargetProperties); }
/// <summary> /// 保存图片 /// </summary> /// <param name="wicFactory"></param> /// <param name="wicBitmap"></param> /// <param name="outputStream"></param> static void SaveD2DBitmap(SharpDX.WIC.ImagingFactory wicFactory, SharpDX.WIC.Bitmap wicBitmap, Stream outputStream) { using var encoder = new SharpDX.WIC.BitmapEncoder(wicFactory, SharpDX.WIC.ContainerFormatGuids.Png); encoder.Initialize(outputStream); using var frame = new SharpDX.WIC.BitmapFrameEncode(encoder); frame.Initialize(); frame.SetSize(wicBitmap.Size.Width, wicBitmap.Size.Height); var pixelFormat = wicBitmap.PixelFormat; frame.SetPixelFormat(ref pixelFormat); frame.WriteSource(wicBitmap); frame.Commit(); encoder.Commit(); }
public static void SaveBitmap(DeviceManager dev, SharpDX.WIC.Bitmap bm, string filename) { System.Diagnostics.Debug.Assert(bm != null); Guid containerFormat = Guid.Empty; string lowerName = filename.ToLower(); if (lowerName.Contains(".png")) { containerFormat = SharpDX.WIC.ContainerFormatGuids.Png; } else if (lowerName.Contains(".bmp")) { containerFormat = SharpDX.WIC.ContainerFormatGuids.Bmp; } else if (lowerName.Contains(".jpg")) { containerFormat = SharpDX.WIC.ContainerFormatGuids.Jpeg; } else if (lowerName.Contains(".jpeg")) { containerFormat = SharpDX.WIC.ContainerFormatGuids.Jpeg; } else if (lowerName.Contains(".tif")) { containerFormat = SharpDX.WIC.ContainerFormatGuids.Tiff; } else if (lowerName.Contains(".gif")) { containerFormat = SharpDX.WIC.ContainerFormatGuids.Gif; } Guid format = bm.PixelFormat; using (var stream = System.IO.File.OpenWrite(filename)) { stream.Position = 0; using (SharpDX.WIC.BitmapEncoder enc = new SharpDX.WIC.BitmapEncoder(dev.WICFactory, containerFormat, stream)) using (SharpDX.WIC.BitmapFrameEncode bfe = new SharpDX.WIC.BitmapFrameEncode(enc)) { bfe.Initialize(); bfe.SetPixelFormat(ref format); bfe.SetSize(bm.Size.Width, bm.Size.Height); bfe.WriteSource(bm); bfe.Commit(); enc.Commit(); } } }
public static Bitmap CopyWicBitmapToBitmap(WicBitmap wicBitmap, int width, int height, object wicBitmapLock) { var pixelData = new byte[width * height * 4]; lock (wicBitmapLock) { wicBitmap.CopyPixels(pixelData, width * 4); } var bitmap = new Bitmap(width, height); var lockRectangle = new Rectangle(0, 0, width, height); var pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb; var lockFlags = ImageLockMode.WriteOnly; var bitmapData = bitmap.LockBits(lockRectangle, lockFlags, pixelFormat); Marshal.Copy(pixelData, 0, bitmapData.Scan0, pixelData.Length); bitmap.UnlockBits(bitmapData); return(bitmap); }
Bitmap CreateNewBitmap(UBitmap b) { // FIXME arg this class is slow! System.Drawing.Bitmap bm; if (b.bitmapData != null) { System.IO.MemoryStream ms = new System.IO.MemoryStream(b.bitmapData); bm = new System.Drawing.Bitmap(ms); } else { bm = new System.Drawing.Bitmap(b.bitmapFile); } SharpDXLib.WIC.Bitmap wbm = new SharpDXLib.WIC.Bitmap(wicFact, bm, SharpDXLib.WIC.BitmapAlphaChannelOption.UseAlpha); var bmd = Bitmap.FromWicBitmap(realRenderer.renderTarget, wbm); realRenderer.renderTarget.Disposed += new EventHandler <EventArgs>((o, e) => b.Invalidate()); return(bmd); }
/// <summary> /// Creates an instance of D2dWicGraphics that can be used for rendering to off screen surfaces /// and can be copied to main memory</summary> /// <param name="width">Width of the off screen surface.</param> /// <param name="height">Height of the off screen surface.</param> /// <returns>D2dWicGraphics that can be used for rendering to off screen surfaces</returns> public static D2dWicGraphics CreateWicGraphics(int width, int height) { var wicBitmap = new SharpDX.WIC.Bitmap(s_wicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); var rtprops = new RenderTargetProperties { Type = RenderTargetType.Default, DpiX = 96.0f, DpiY = 96.0f, PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Unknown), Usage = RenderTargetUsage.None, MinLevel = FeatureLevel.Level_DEFAULT }; var rt = new WicRenderTarget(s_d2dFactory, wicBitmap, rtprops); return(new D2dWicGraphics(rt, wicBitmap)); }
public Direct2DGraphics(int width, int height) : this() { var wicFactory = new SharpDX.WIC.ImagingFactory(); bitmap = DisposeLater(new SharpDX.WIC.Bitmap( wicFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapCreateCacheOption.CacheOnDemand)); dc = DisposeLater(new WicRenderTarget( d2dFactory, bitmap, new RenderTargetProperties( RenderTargetType.Default, new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied), d2dFactory.DesktopDpi.Width, d2dFactory.DesktopDpi.Height, RenderTargetUsage.None, FeatureLevel.Level_DEFAULT))); Initialize(); }
public void Dispose() { if (_renderTexture != null) { _renderTexture.Dispose(); _renderTexture = null; } if (_wicBitmap != null) { _wicBitmap.Dispose(); _wicBitmap = null; } if (_wicRenderTarget != null) { _wicRenderTarget.Dispose(); _wicRenderTarget = null; } if (_sceneColorBrush != null) { _sceneColorBrush.Dispose(); _sceneColorBrush = null; } if (OverlayBufferRes != null) { OverlayBufferRes.Dispose(); OverlayBufferRes = null; } if (_textFormat != null) { _textFormat.Dispose(); _textFormat = null; } }
public RenderedImage(Bitmap bitmap, int bitmapWidth, int bitmapHeight) { _bitmap = bitmap; BitmapWidth = bitmapWidth; BitmapHeight = bitmapHeight; }
public Character(Theatre father, string name, string template, bool canshow = true, double?time = null, bool isAscy = true, double vel_x = 0, double vel_y = 0) { var layer = father.CharacterLayer; m_father = father; ManualResetEvent msbn = new ManualResetEvent(false); _name = name; if (time == null) { time = SharedSetting.SwitchSpeed; } layer.Dispatcher.Invoke(new Action(() => { whereIsShowed = layer; Init_action = Stage.LoadBitmap(template); shower = new Image { Width = Init_action.Size.Width * (layer.Height / Init_action.Size.Height), Height = layer.Height, VerticalAlignment = VerticalAlignment.Bottom, Stretch = Stretch.Fill, Margin = new Thickness(vel_x, 0, 0, vel_y) }; if (time != 0 || !canshow) { shower.Opacity = 0; } whereIsShowed.Children.Add(shower); Direct2DImage direct2DImage = new Direct2DImage(new Size2((int)shower.Width, (int)shower.Height), 30); direct2DImage.DrawProc += (View, Souce, Width, Height) => { D2DBitmap m_bp = D2DBitmap.FromWicBitmap(View.View, Init_action, new BitmapProperties1(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied))); View.View.BeginDraw(); View.View.Clear(null); View.View.DrawBitmap(m_bp, new RawRectangleF(0, 0, Width, Height), 1, SharpDX.Direct2D1.BitmapInterpolationMode.Linear, new RawRectangleF(0, 0, Init_action.Size.Width, Init_action.Size.Height)); View.View.EndDraw(); m_bp.Dispose(); return(DrawProcResult.Death); }; direct2DImage.Disposing += (e, s) => { Last_Draw = s.LastDraw; msbn.Set(); }; direct2DImage.DrawStartup(shower); })); msbn.WaitOne(); msbn.Dispose(); if (!canshow) { return; } EasyAmal aml = new EasyAmal(shower, "(Opacity)", 0.0, 1.0, (double)time); aml.Start(isAscy); }//已经确认过安全的代码,再次修改需要小心
}//已经确认过安全的代码,再次修改需要小心 public void SwitchTo(int area, int index, double?time = null, bool isAysn = false) { if (time == null) { time = SharedSetting.TextSpeed; } if (time < 1.0 / 30.0 && time != 0) { throw new Exception("time can not be less than 1/30s"); } Rect targetArea = ChAreas[area].area; WICBitmap rost_pitch = ChAreas[area].switches[index]; shower.Dispatcher.Invoke(() => { dx_switch = new Direct2DImage(new Size2((int)shower.Width, (int)shower.Height), 30)//////////////AAA { }; }); double soul_rate = 0; shower.Dispatcher.Invoke(() => { soul_rate = whereIsShowed.Height / Init_action.Size.Height; }); dx_switch.FirstDraw += (e, v, w, h) => { D2DBitmap m_ipq = D2DBitmap.FromWicBitmap(e.View, Last_Draw);/////////////////AAA e.View.BeginDraw(); e.View.Clear(null); e.View.DrawBitmap(m_ipq, new RawRectangleF(0, 0, w, h), 1, SharpDX.Direct2D1.BitmapInterpolationMode.Linear, new RawRectangleF(0, 0, Last_Draw.Size.Width, Last_Draw.Size.Height)); e.View.EndDraw(); m_ipq.Dispose();//////////////////BB return; }; ManualResetEvent msbn = new ManualResetEvent(false);//////////////////AAA if (time == 0) { dx_switch.DrawProc += (e, v, w, h) => { D2DBitmap m_ipq = D2DBitmap.FromWicBitmap(e.View, Init_action); D2DBitmap m_ipq2 = D2DBitmap.FromWicBitmap(e.View, rost_pitch); e.View.BeginDraw(); e.View.DrawBitmap(m_ipq, new RawRectangleF(0, 0, w, h), 1, SharpDX.Direct2D1.BitmapInterpolationMode.Linear, new RawRectangleF(0, 0, Init_action.Size.Width, Init_action.Size.Height)); e.View.DrawBitmap(m_ipq2, new RawRectangleF( (float)(targetArea.Left * soul_rate), (float)(targetArea.Top * soul_rate), (float)(targetArea.Right * soul_rate), (float)(targetArea.Bottom * soul_rate) ), 1, SharpDX.Direct2D1.BitmapInterpolationMode.Linear, new RawRectangleF(0, 0, rost_pitch.Size.Width, rost_pitch.Size.Height)); e.View.EndDraw(); m_ipq.Dispose(); m_ipq2.Dispose(); if (!isAysn) { msbn.Set(); } return(DrawProcResult.Death); }; dx_switch.Disposing += (e, s) => { if (Last_Draw != null) { if (!Last_Draw.IsDisposed) { Last_Draw.Dispose(); } } Last_Draw = s.LastDraw; }; } else { double interrase = 1 / ((double)time * 30); double varb = 0; D2DBitmap m_ipq = D2DBitmap.FromWicBitmap(dx_switch.m_d2d_info.View, Last_Draw); //////////////AAA D2DBitmap m_ipq2 = D2DBitmap.FromWicBitmap(dx_switch.m_d2d_info.View, rost_pitch); ///////////////AAA D2DBitmap m_ipq3 = D2DBitmap.FromWicBitmap(dx_switch.m_d2d_info.View, Init_action); dx_switch.DrawProc += (e, v, w, h) => { e.View.BeginDraw(); e.View.Clear(null); #region 两次绘图 e.View.DrawBitmap(m_ipq, new RawRectangleF(0, 0, w, h), 1, SharpDX.Direct2D1.BitmapInterpolationMode.Linear, new RawRectangleF(0, 0, Last_Draw.Size.Width, Last_Draw.Size.Height)); e.View.DrawBitmap(m_ipq3, new RawRectangleF(0, 0, w, h), (float)varb, SharpDX.Direct2D1.BitmapInterpolationMode.Linear, new RawRectangleF(0, 0, Init_action.Size.Width, Init_action.Size.Height)); e.View.DrawBitmap(m_ipq2, new RawRectangleF( (float)(targetArea.Left * soul_rate), (float)(targetArea.Top * soul_rate), (float)(targetArea.Right * soul_rate), (float)(targetArea.Bottom * soul_rate) ), (float)varb, SharpDX.Direct2D1.BitmapInterpolationMode.Linear, new RawRectangleF(0, 0, rost_pitch.Size.Width, rost_pitch.Size.Height)); #endregion e.View.EndDraw(); if (varb > 1) { m_ipq.Dispose(); m_ipq2.Dispose(); if (!isAysn) { msbn.Set(); } return(DrawProcResult.Death);//////////////////BB } varb += interrase; return(DrawProcResult.Normal); }; dx_switch.Disposing += (e, s) => { if (Last_Draw != null) { if (!Last_Draw.IsDisposed) { Last_Draw.Dispose(); } } Last_Draw = s.LastDraw; m_ipq.Dispose(); //////////////////BB m_ipq2.Dispose(); //////////////////BB m_ipq3.Dispose(); }; } // Last_Draw.Dispose(); shower.Dispatcher.Invoke(() => { dx_switch.DrawStartup(shower); }); if (!isAysn) { msbn.WaitOne(); msbn.Dispose();//////////////////BB } msbn.Dispose(); }//已经确认过安全的代码,再次修改需要小心
internal D2dWicGraphics(WicRenderTarget renderTarget, SharpDX.WIC.Bitmap wicBitmap) : base(renderTarget) { m_wicBitmap = wicBitmap; }
/// <summary> /// Copies to a stream using WIC. The format is converted if necessary. /// </summary> /// <param name="context"></param> /// <param name="texture"></param> /// <param name="outputFormat"></param> /// <param name="stream"></param> public void ToStream(SharpDX.Direct3D11.DeviceContext context, Texture2D texture, ImageFormat outputFormat, Stream stream) { if (wicFactory == null) { wicFactory = ToDispose(new SharpDX.WIC.ImagingFactory2()); } DataStream dataStream; var dataBox = context.MapSubresource( texture, 0, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None, out dataStream); try { var dataRectangle = new DataRectangle { DataPointer = dataStream.DataPointer, Pitch = dataBox.RowPitch }; var format = PixelFormatFromFormat(texture.Description.Format); if (format == Guid.Empty) { return; } using (var bitmap = new SharpDX.WIC.Bitmap( wicFactory, texture.Description.Width, texture.Description.Height, format, dataRectangle)) { stream.Position = 0; SharpDX.WIC.BitmapEncoder bitmapEncoder = null; switch (outputFormat) { case ImageFormat.Bitmap: bitmapEncoder = new SharpDX.WIC.BmpBitmapEncoder(wicFactory, stream); break; case ImageFormat.Jpeg: bitmapEncoder = new SharpDX.WIC.JpegBitmapEncoder(wicFactory, stream); break; case ImageFormat.Png: bitmapEncoder = new SharpDX.WIC.PngBitmapEncoder(wicFactory, stream); break; default: return; } try { using (var bitmapFrameEncode = new SharpDX.WIC.BitmapFrameEncode(bitmapEncoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height); var pixelFormat = format; bitmapFrameEncode.SetPixelFormat(ref pixelFormat); if (pixelFormat != format) { // IWICFormatConverter var converter = new SharpDX.WIC.FormatConverter(wicFactory); if (converter.CanConvert(format, pixelFormat)) { converter.Initialize(bitmap, SharpDX.WIC.PixelFormat.Format24bppBGR, SharpDX.WIC.BitmapDitherType.None, null, 0, SharpDX.WIC.BitmapPaletteType.MedianCut); bitmapFrameEncode.SetPixelFormat(ref pixelFormat); bitmapFrameEncode.WriteSource(converter); } else { this.DebugMessage(string.Format("Unable to convert Direct3D texture format {0} to a suitable WIC format", texture.Description.Format.ToString())); return; } } else { bitmapFrameEncode.WriteSource(bitmap); } bitmapFrameEncode.Commit(); bitmapEncoder.Commit(); } } finally { bitmapEncoder.Dispose(); } } } finally { context.UnmapSubresource(texture, 0); } }
/// <summary> /// 制作背景图片 /// </summary> /// <param name="pictureFileFullPath"></param> /// <param name="fileDirectory"></param> /// <param name="textContent"></param> /// <returns></returns> static string GenerateWallPaper(string pictureFileFullPath, string fileDirectory, string textContent) { var wic = new SharpDX.WIC.ImagingFactory2(); var d2d = new SharpDX.Direct2D1.Factory(); float dpi = d2d.DesktopDpi.Width; Size2 size = new Size2(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); SharpDX.WIC.FormatConverter image = CreateWicImage(wic, pictureFileFullPath); using var wicBitmap = new SharpDX.WIC.Bitmap(wic, size.Width, size.Height, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapCreateCacheOption.CacheOnDemand); using var target = new SharpDX.Direct2D1.WicRenderTarget(d2d, wicBitmap, new SharpDX.Direct2D1.RenderTargetProperties()); using var dc = target.QueryInterface <SharpDX.Direct2D1.DeviceContext>(); using var bmpPicture = SharpDX.Direct2D1.Bitmap.FromWicBitmap(target, image); using var dwriteFactory = new SharpDX.DirectWrite.Factory(); using var brush = new SolidColorBrush(target, SharpDX.Color.LightGoldenrodYellow); using var bmpLayer = new SharpDX.Direct2D1.Bitmap1(dc, target.PixelSize, new SharpDX.Direct2D1.BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), dpi, dpi, SharpDX.Direct2D1.BitmapOptions.Target)); var oldTarget = dc.Target; dc.Target = bmpLayer; target.BeginDraw(); { var textFormat = new SharpDX.DirectWrite.TextFormat(dwriteFactory, "Tahoma", size.Height / 27); // draw textContent { var textLayout = new SharpDX.DirectWrite.TextLayout(dwriteFactory, textContent, textFormat, target.Size.Width * 0.75f, float.MaxValue); var center = new Vector2((target.Size.Width - textLayout.Metrics.Width) / 2, (target.Size.Height - textLayout.Metrics.Height) / 2); target.DrawTextLayout(new Vector2(center.X, center.Y), textLayout, brush); } //{ // // draw otherContent // var textLayout = new SharpDX.DirectWrite.TextLayout(dwriteFactory, chinese, textFormat, target.Size.Width * 0.75f, float.MaxValue); // var center = new Vector2((target.Size.Width - textLayout.Metrics.Width) / 2, target.Size.Height - textLayout.Metrics.Height - size.Height / 18); // target.DrawTextLayout(new Vector2(center.X, center.Y), textLayout, brush); //} } target.EndDraw(); // shadow var shadow = new SharpDX.Direct2D1.Effects.Shadow(dc); shadow.SetInput(0, bmpLayer, new RawBool(false)); dc.Target = oldTarget; target.BeginDraw(); { target.DrawBitmap(bmpPicture, new SharpDX.RectangleF(0, 0, target.Size.Width, target.Size.Height), 1.0f, BitmapInterpolationMode.Linear); dc.DrawImage(shadow, new Vector2(size.Height / 150.0f, size.Height / 150.0f)); dc.UnitMode = UnitMode.Pixels; target.DrawBitmap(bmpLayer, 1.0f, BitmapInterpolationMode.Linear); } target.EndDraw(); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } string wallpaperFileName = fileDirectory + DateTime.Now.ToString("yyyyMMdd") + "_" + "wallpaper.png"; using var wallpaperStream = File.OpenWrite(wallpaperFileName); SaveD2DBitmap(wic, wicBitmap, wallpaperStream); wallpaperStream.Close(); return(wallpaperFileName); }
public static SharpDX.WIC.Bitmap SaveToWICBitmap(DeviceManager deviceManager, Texture2D source) { var device = deviceManager.Direct3DDevice; var context = deviceManager.Direct3DContext; var txDesc = source.Description; txDesc.Usage = ResourceUsage.Staging; txDesc.CpuAccessFlags = CpuAccessFlags.Read; txDesc.BindFlags = BindFlags.None; txDesc.OptionFlags = ResourceOptionFlags.None; txDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0); Guid pixelFormat = PixelFormatFromFormat(txDesc.Format); if (pixelFormat == Guid.Empty) { return(null); } System.Diagnostics.Debug.Assert(BitsPerPixel(txDesc.Format) == SharpDX.WIC.PixelFormat.GetBitsPerPixel(pixelFormat), "Error with DXGI.Format -> PixelFormat"); using (var dest = new Texture2D(device, txDesc)) { if (source.Description.SampleDescription.Count > 1 || source.Description.SampleDescription.Quality > 0) { // In order to copy a multisampled texture to a CPU readable texture, it must first be resolved into a GPU only Texture // Initialize a target to resolve multi-sampled render target var resolvedDesc = source.Description; resolvedDesc.BindFlags = BindFlags.ShaderResource; resolvedDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0); // if depth stencil needs to be typeless if (resolvedDesc.Format == SharpDX.DXGI.Format.D24_UNorm_S8_UInt) { resolvedDesc.Format = SharpDX.DXGI.Format.R24G8_Typeless; } using (var resolvedTarget = new Texture2D(device, resolvedDesc)) { CopyToTexture(context, source, resolvedTarget); // Now we can copy to the destination CopyToTexture(context, source, dest); } } else { CopyToTexture(context, source, dest); } var sourceData = context.MapSubresource(dest, 0, MapMode.Read, MapFlags.None); var encoder = new SharpDX.WIC.PngBitmapEncoder(deviceManager.WICFactory); var formatConverter = new SharpDX.WIC.FormatConverter(deviceManager.WICFactory); SharpDX.WIC.Bitmap bm = new SharpDX.WIC.Bitmap(deviceManager.WICFactory, txDesc.Width, txDesc.Height, pixelFormat, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); var bytesPerPixel = BitsPerPixel(txDesc.Format) / 8; using (var l = bm.Lock(SharpDX.WIC.BitmapLockFlags.Write)) { var destPtr = l.Data.DataPointer; var sourcePtr = sourceData.DataPointer; for (int y = 0; y < bm.Size.Height; y++) { SharpDX.Utilities.CopyMemory(destPtr, sourcePtr, bm.Size.Width * bytesPerPixel); sourcePtr = IntPtr.Add(sourcePtr, sourceData.RowPitch); destPtr = IntPtr.Add(destPtr, l.Data.Pitch); } } context.UnmapSubresource(dest, 0); return(bm); } }
public static SharpDX.WIC.Bitmap SaveToWICBitmap(DeviceManager deviceManager, Texture2D source) { var device = deviceManager.Direct3DDevice; var context = deviceManager.Direct3DContext; var txDesc = source.Description; txDesc.Usage = ResourceUsage.Staging; txDesc.CpuAccessFlags = CpuAccessFlags.Read; txDesc.BindFlags = BindFlags.None; txDesc.OptionFlags = ResourceOptionFlags.None; txDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0); Guid pixelFormat = PixelFormatFromFormat(txDesc.Format); if (pixelFormat == Guid.Empty) { return null; } System.Diagnostics.Debug.Assert(BitsPerPixel(txDesc.Format) == SharpDX.WIC.PixelFormat.GetBitsPerPixel(pixelFormat), "Error with DXGI.Format -> PixelFormat"); using (var dest = new Texture2D(device, txDesc)) { if (source.Description.SampleDescription.Count > 1 || source.Description.SampleDescription.Quality > 0) { // In order to copy a multisampled texture to a CPU readable texture, it must first be resolved into a GPU only Texture // Initialize a target to resolve multi-sampled render target var resolvedDesc = source.Description; resolvedDesc.BindFlags = BindFlags.ShaderResource; resolvedDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0); // if depth stencil needs to be typeless if (resolvedDesc.Format == SharpDX.DXGI.Format.D24_UNorm_S8_UInt) resolvedDesc.Format = SharpDX.DXGI.Format.R24G8_Typeless; using (var resolvedTarget = new Texture2D(device, resolvedDesc)) { CopyToTexture(context, source, resolvedTarget); // Now we can copy to the destination CopyToTexture(context, source, dest); } } else CopyToTexture(context, source, dest); var sourceData = context.MapSubresource(dest, 0, MapMode.Read, MapFlags.None); var encoder = new SharpDX.WIC.PngBitmapEncoder(deviceManager.WICFactory); var formatConverter = new SharpDX.WIC.FormatConverter(deviceManager.WICFactory); SharpDX.WIC.Bitmap bm = new SharpDX.WIC.Bitmap(deviceManager.WICFactory, txDesc.Width, txDesc.Height, pixelFormat, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); var bytesPerPixel = BitsPerPixel(txDesc.Format) / 8; using (var l = bm.Lock(SharpDX.WIC.BitmapLockFlags.Write)) { var destPtr = l.Data.DataPointer; var sourcePtr = sourceData.DataPointer; for (int y = 0; y < bm.Size.Height; y++) { SharpDX.Utilities.CopyMemory(destPtr, sourcePtr, bm.Size.Width * bytesPerPixel); sourcePtr = IntPtr.Add(sourcePtr, sourceData.RowPitch); destPtr = IntPtr.Add(destPtr, l.Data.Pitch); } } context.UnmapSubresource(dest, 0); return bm; } }
public DUIBitmap(int width, int height) { this.wicBitmap = new SharpDX.WIC.Bitmap(imagingFactory, width, height, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad); }
public static BitmapSource GetBitmapSourceFromWicBitmap(WicBitmap wicBitmap, int width, int height, object wicBitmapLock) { Bitmap bitmap = CopyWicBitmapToBitmap(wicBitmap, width, height, wicBitmapLock); return(ConvertBitmapToSource(bitmap)); }