public Object LoadImage(String filename) { Object image = null; if (File.Exists(filename)) { using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(filename)) { System.Drawing.Imaging.BitmapData bData = bmp.LockBits( new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb ); SharpDX.DataStream stream = new SharpDX.DataStream(bData.Scan0, bData.Stride * bData.Height, true, false); SharpDX.Direct2D1.PixelFormat pFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied); SharpDX.Direct2D1.BitmapProperties bProps = new SharpDX.Direct2D1.BitmapProperties(pFormat); image = new SharpDX.Direct2D1.Bitmap(d2dRenderTarget, new Size2(bmp.Width, bmp.Height), stream, bData.Stride, bProps); bmp.UnlockBits(bData); stream.Dispose(); } } return(image); }
private void DrawTexture(SharpDX.Direct2D1.RenderTarget renderTarget, Texture2D texture) { using (var surf = texture.QueryInterface <SharpDX.DXGI.Surface1>()) { var prop = new SharpDX.Direct2D1.BitmapProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)); SharpDX.Direct2D1.Bitmap screenBits = new SharpDX.Direct2D1.Bitmap(renderTarget, surf, prop); try { var srcDecr = surf.Description; float srcWidth = srcDecr.Width; float srcHeight = srcDecr.Height; float destX = 0; float destY = 0; float destWidth = DestSize.Width; float destHeight = DestSize.Height; float scaleX = destWidth / srcWidth; float scaleY = destHeight / srcHeight; if (AspectRatio) { if (scaleY < scaleX) { scaleX = scaleY; destX = ((destWidth - srcWidth * scaleX) / 2); } else { scaleY = scaleX; destY = ((destHeight - srcHeight * scaleY) / 2); } } destWidth = srcWidth * scaleX; destHeight = srcHeight * scaleY; var destRect = new SharpDX.Mathematics.Interop.RawRectangleF { Left = destX, Right = destX + destWidth, Top = destY, Bottom = destY + destHeight, }; renderTarget.DrawBitmap(screenBits, destRect, 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear); } finally { screenBits?.Dispose(); } } }
public static SharpDX.Direct2D1.Bitmap ToBitmap(DUIRenderTarget renderTarget, Bitmap bitmap) { if (bitmap == null) { throw new ArgumentNullException("bitmap"); } System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat); var dataStream = new SharpDX.DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false); var properties = new SharpDX.Direct2D1.BitmapProperties { PixelFormat = new SharpDX.Direct2D1.PixelFormat { Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode = SharpDX.Direct2D1.AlphaMode.Premultiplied } }; // ToDo apply scaling here! //var scaler = new SharpDX.WIC.BitmapScaler(renderTarget.Factory.NativePointer); //scaler. //Load the image from the gdi resource var result = new SharpDX.Direct2D1.Bitmap(renderTarget, new SharpDX.Size2(bitmap.Width, bitmap.Height), dataStream, bitmapData.Stride, properties); bitmap.UnlockBits(bitmapData); return(result); //System.Drawing.Bitmap desBitmap;//预定义要是使用的bitmap ////如果原始的图像像素格式不是32位带alpha通道,需要转换为32位带alpha通道的格式,否则无法和Direct2D的格式对应 //if (bitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppPArgb) //{ // desBitmap = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); // using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(desBitmap)) // { // g.DrawImage(bitmap, new System.Drawing.Rectangle(0, 0, desBitmap.Width, desBitmap.Height), new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.GraphicsUnit.Pixel); // } //} //else //{ // desBitmap = bitmap; //} ////直接内存copy会非常快 ////如果使用循环逐点转换会非常慢 //System.Drawing.Imaging.BitmapData bmpData = desBitmap.LockBits(new System.Drawing.Rectangle(0, 0, desBitmap.Width, desBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, desBitmap.PixelFormat); //int numBytes = bmpData.Stride * desBitmap.Height; //byte[] byteData = new byte[numBytes]; //IntPtr ptr = bmpData.Scan0; //System.Runtime.InteropServices.Marshal.Copy(ptr, byteData, 0, numBytes); //desBitmap.UnlockBits(bmpData); //SharpDX.Direct2D1.PixelFormat pixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied); //SharpDX.Direct2D1.BitmapProperties bp = new SharpDX.Direct2D1.BitmapProperties(pixelFormat, desBitmap.HorizontalResolution, desBitmap.VerticalResolution); //SharpDX.Direct2D1.Bitmap tempBitmap = new SharpDX.Direct2D1.Bitmap(renderTarget, new SharpDX.Size2(desBitmap.Width, desBitmap.Height), bp); //tempBitmap.CopyFromMemory(byteData, bmpData.Stride); //return tempBitmap; }
internal D2dBitmap(D2dGraphics owner, int width, int height) { if (width < 1 || height < 1) { throw new ArgumentOutOfRangeException("Width and height must be greater than zero"); } m_owner = owner; var props = new SharpDX.Direct2D1.BitmapProperties(); props.DpiX = m_owner.DotsPerInch.Width; props.DpiY = m_owner.DotsPerInch.Height; props.PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied); m_nativeBitmap = new SharpDX.Direct2D1.Bitmap(m_owner.D2dRenderTarget, new Size2(width, height), props); m_owner.RecreateResources += RecreateResources; m_rtNumber = owner.RenderTargetNumber; }
internal void Create() { if (m_bitmap == null) { return; } if (m_nativeBitmap != null) { m_nativeBitmap.Dispose(); m_nativeBitmap = null; } var props = new SharpDX.Direct2D1.BitmapProperties(); props.DpiX = m_bitmap.HorizontalResolution; props.DpiY = m_bitmap.VerticalResolution; props.PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied); m_nativeBitmap = new SharpDX.Direct2D1.Bitmap(m_owner.D2dRenderTarget, new Size2(m_bitmap.Width, m_bitmap.Height), props); Update(); }
private SharpDX.Direct2D1.Bitmap GDIBitmapToSharpDXBitmap(System.Drawing.Bitmap bitmap, SharpDX.Direct2D1.RenderTarget device) { System.Drawing.Rectangle sourceArea = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height); SharpDX.Direct2D1.BitmapProperties bitmapProperties = new SharpDX.Direct2D1.BitmapProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)); SharpDX.Size2 size = new SharpDX.Size2(bitmap.Width, bitmap.Height); // Transform pixels from GDI's wild ass BGRA to DXGI-compatible RGBA. int stride = bitmap.Width * sizeof(int); using (SharpDX.DataStream pixelStream = new SharpDX.DataStream(bitmap.Height * stride, true, true)) { // Lock the source bitmap. System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(sourceArea, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); // Convert each pixel. for (int y = 0; y < bitmap.Height; ++y) { int offset = bitmapData.Stride * y; for (int x = 0; x < bitmap.Width; ++x) { byte B = Marshal.ReadByte(bitmapData.Scan0, offset++); byte G = Marshal.ReadByte(bitmapData.Scan0, offset++); byte R = Marshal.ReadByte(bitmapData.Scan0, offset++); byte A = Marshal.ReadByte(bitmapData.Scan0, offset++); int rgba = R | (G << 8) | (B << 16) | (A << 24); pixelStream.Write(rgba); } } // Unlock source bitmap now. bitmap.UnlockBits(bitmapData); // Reset stream position for reading. pixelStream.Position = 0; // Create the SharpDX bitmap from the DataStream. return(new SharpDX.Direct2D1.Bitmap(device, size, pixelStream, stride, bitmapProperties)); } }
internal static SharpDX.Direct2D1.Bitmap ToD2DBitmap(this System.Drawing.Bitmap bitmap, SharpDX.Direct2D1.RenderTarget renderTarget) { var sourceArea = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height); var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties( new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)); var size = new SharpDX.Size2(bitmap.Width, bitmap.Height); // Transform pixels from BGRA to RGBA int stride = bitmap.Width * sizeof(int); using (var tempStream = new SharpDX.DataStream(bitmap.Height * stride, true, true)) { // Lock System.Drawing.Bitmap var bitmapData = bitmap.LockBits(sourceArea, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); // Convert all pixels for (int y = 0; y < bitmap.Height; y++) { int offset = bitmapData.Stride * y; for (int x = 0; x < bitmap.Width; x++) { // Not optimized byte B = Marshal.ReadByte(bitmapData.Scan0, offset++); byte G = Marshal.ReadByte(bitmapData.Scan0, offset++); byte R = Marshal.ReadByte(bitmapData.Scan0, offset++); byte A = Marshal.ReadByte(bitmapData.Scan0, offset++); int rgba = B | (G << 8) | (R << 16) | (A << 24); tempStream.Write(rgba); } } bitmap.UnlockBits(bitmapData); tempStream.Position = 0; return new SharpDX.Direct2D1.Bitmap(renderTarget, size, tempStream, stride, bitmapProperties); } }
/// <summary> /// Creates a Direct2D bitmap from a pointer to in-memory source data. /// </summary> /// <param name="renderTarget">an instance of <see cref = "SharpDX.Direct2D1.RenderTarget" /></param> /// <param name="size">The dimension of the bitmap to create in pixels.</param> /// <param name="dataPointer">A pointer to the memory location of the image data, or NULL to create an uninitialized bitmap.</param> /// <param name="pitch">The byte count of each scanline, which is equal to (the image width in pixels * the number of bytes per pixel) + memory padding. If srcData is NULL, this value is ignored. (Note that pitch is also sometimes called stride.)</param> /// <param name="bitmapProperties">The pixel format and dots per inch (DPI) of the bitmap to create.</param> /// <msdn-id>dd371800</msdn-id> /// <unmanaged>HRESULT ID2D1RenderTarget::CreateBitmap([In] D2D_SIZE_U size,[In, Optional] const void* srcData,[In] unsigned int pitch,[In] const D2D1_BITMAP_PROPERTIES* bitmapProperties,[Out, Fast] ID2D1Bitmap** bitmap)</unmanaged> /// <unmanaged-short>ID2D1RenderTarget::CreateBitmap</unmanaged-short> public Bitmap(RenderTarget renderTarget, Size2 size, DataPointer dataPointer, int pitch, SharpDX.Direct2D1.BitmapProperties bitmapProperties) : base(IntPtr.Zero) { renderTarget.CreateBitmap(size, dataPointer == DataPointer.Zero ? IntPtr.Zero : dataPointer.Pointer, pitch, bitmapProperties, this); }
/// <summary> /// Creates a Direct2D bitmap from a pointer to in-memory source data. /// </summary> /// <param name="renderTarget">an instance of <see cref = "SharpDX.Direct2D1.RenderTarget" /></param> /// <param name="size">The dimension of the bitmap to create in pixels.</param> /// <param name="bitmapProperties">The pixel format and dots per inch (DPI) of the bitmap to create.</param> /// <msdn-id>dd371800</msdn-id> /// <unmanaged>HRESULT ID2D1RenderTarget::CreateBitmap([In] D2D_SIZE_U size,[In, Optional] const void* srcData,[In] unsigned int pitch,[In] const D2D1_BITMAP_PROPERTIES* bitmapProperties,[Out, Fast] ID2D1Bitmap** bitmap)</unmanaged> /// <unmanaged-short>ID2D1RenderTarget::CreateBitmap</unmanaged-short> public Bitmap(RenderTarget renderTarget, Size2 size, SharpDX.Direct2D1.BitmapProperties bitmapProperties) : this(renderTarget, size, DataPointer.Zero, 0, bitmapProperties) { }
/// <summary> /// Creates a Bitmap from a WIC bitmap. /// </summary> /// <param name="renderTarget">The render target.</param> /// <param name="wicBitmap">The WIC bitmap.</param> /// <param name="bitmapProperties">The bitmap properties.</param> /// <returns></returns> /// <msdn-id>dd371797</msdn-id> /// <unmanaged>HRESULT ID2D1RenderTarget::CreateBitmapFromWicBitmap([In] IWICBitmapSource* wicBitmapSource,[In, Optional] const D2D1_BITMAP_PROPERTIES* bitmapProperties,[Out] ID2D1Bitmap** bitmap)</unmanaged> /// <unmanaged-short>ID2D1RenderTarget::CreateBitmapFromWicBitmap</unmanaged-short> public static Bitmap FromWicBitmap(RenderTarget renderTarget, WIC.BitmapSource wicBitmap, SharpDX.Direct2D1.BitmapProperties bitmapProperties) { Bitmap bitmap; renderTarget.CreateBitmapFromWicBitmap(wicBitmap, bitmapProperties, out bitmap); return(bitmap); }
/// <summary> /// Creates a Direct2D bitmap from a pointer to in-memory source data. /// </summary> /// <param name="renderTarget">an instance of <see cref = "SharpDX.Direct2D1.RenderTarget" /></param> /// <param name="size">The dimension of the bitmap to create in pixels.</param> /// <param name="pixelDatas">A pointer to an array of pixel data. The size of the array must be equal to sizeof(pixel) * Size.Width * Height.</param> /// <param name="bitmapProperties">The pixel format and dots per inch (DPI) of the bitmap to create.</param> /// <msdn-id>dd371800</msdn-id> /// <unmanaged>HRESULT ID2D1RenderTarget::CreateBitmap([In] D2D_SIZE_U size,[In, Optional] const void* srcData,[In] unsigned int pitch,[In] const D2D1_BITMAP_PROPERTIES* bitmapProperties,[Out, Fast] ID2D1Bitmap** bitmap)</unmanaged> /// <unmanaged-short>ID2D1RenderTarget::CreateBitmap</unmanaged-short> public unsafe static Bitmap New <T>(RenderTarget renderTarget, Size2 size, T[] pixelDatas, SharpDX.Direct2D1.BitmapProperties bitmapProperties) where T : struct { var sizeOfBitmap = pixelDatas.Length * Utilities.SizeOf <T>(); var expectedSize = size.Width * size.Height * FormatHelper.SizeOfInBytes(bitmapProperties.PixelFormat.Format); if (sizeOfBitmap != expectedSize) { throw new ArgumentException("Invalid size of pixelDatas. Must be equal to sizeof(T) == sizeof(PixelFormat.Format) and Width * Height elements"); } return(new Bitmap(renderTarget, size, new DataPointer((IntPtr)Interop.Fixed(pixelDatas), sizeOfBitmap), size.Width * FormatHelper.SizeOfInBytes(bitmapProperties.PixelFormat.Format), bitmapProperties)); }
internal void Create() { if (m_bitmap == null) return; if (m_nativeBitmap != null) { m_nativeBitmap.Dispose(); m_nativeBitmap = null; } var props = new SharpDX.Direct2D1.BitmapProperties(); props.DpiX = m_bitmap.HorizontalResolution; props.DpiY = m_bitmap.VerticalResolution; props.PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied); m_nativeBitmap = new SharpDX.Direct2D1.Bitmap(m_owner.D2dRenderTarget, new DrawingSize(m_bitmap.Width, m_bitmap.Height), props); Update(); }
internal D2dBitmap(D2dGraphics owner, int width, int height) { if (width < 1 || height < 1) throw new ArgumentOutOfRangeException("Width and height must be greater than zero"); m_owner = owner; var props = new SharpDX.Direct2D1.BitmapProperties(); props.DpiX = m_owner.DotsPerInch.Width; props.DpiY = m_owner.DotsPerInch.Height; props.PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied); m_nativeBitmap = new SharpDX.Direct2D1.Bitmap(m_owner.D2dRenderTarget, new Size2(width, height), props); m_owner.RecreateResources += RecreateResources; m_rtNumber = owner.RenderTargetNumber; }