} // RotateImage() public void Resize(Size newSize) { this.imageGDI = new System.Drawing.Bitmap(this.imageGDI, newSize); this.imageDirect2D = GetDirect2DBitmapWithSharpDX(MyDevice2D.render[this.idRender], this.imageGDI); this.size = new SizeF(newSize.Width, newSize.Height); } // Resize()
} // Resize() internal static Bitmap GetDirect2DBitmapWithSharpDX(RenderTarget rt, System.Drawing.Bitmap image) { if (image == null) { throw new ArgumentNullException("image"); } if (image.PixelFormat != GdiPixelFormat.Format32bppArgb) { return(null); } var imageData = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat); var dataStream = new DataStream(imageData.Scan0, imageData.Stride * imageData.Height, true, false); var properties = new BitmapProperties { PixelFormat = new SharpDX.Direct2D1.PixelFormat { Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode = AlphaMode.Premultiplied } }; //Load the image from the gdi resource var bitmapDirect2D = new Bitmap(rt, new Size2(image.Width, image.Height), dataStream, imageData.Stride, properties); image.UnlockBits(imageData); return(bitmapDirect2D); } // GetDirect2DBitmapWithSharpDX()
public Bitmap ConvertBitmap(System.Drawing.Bitmap bmp) { System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits( new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); DataStream stream = new DataStream(bmpData.Scan0, bmpData.Stride * bmpData.Height, true, false); PixelFormat pFormat = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied); BitmapProperties bmpProps = new BitmapProperties(pFormat); Bitmap result = new Bitmap( renderTarget, new Size2(bmp.Width, bmp.Height), stream, bmpData.Stride, bmpProps); bmp.UnlockBits(bmpData); stream.Dispose(); bmp.Dispose(); return(result); }
/// <summary> /// Initializes a new DirectXTexture class. /// </summary> /// <param name="bmp">The Bitmap.</param> internal DirectXTexture(System.Drawing.Bitmap bmp) { RawBitmap = (System.Drawing.Bitmap)bmp.Clone(); _width = bmp.Width; _height = bmp.Height; var sourceArea = new Rectangle(0, 0, bmp.Width, bmp.Height); var bitmapProperties = new BitmapProperties( new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), 96, 96); var size = new Size2(bmp.Width, bmp.Height); int stride = bmp.Width * sizeof(int); using (var tempStream = new DataStream(bmp.Height * stride, true, true)) { BitmapData bitmapData = bmp.LockBits(sourceArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); for (int y = 0; y < bmp.Height; y++) { int offset = bitmapData.Stride * y; for (int x = 0; x < bmp.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); tempStream.Write(rgba); } } bmp.UnlockBits(bitmapData); tempStream.Position = 0; _bmp = new Bitmap(DirectXHelper.RenderTarget, size, tempStream, stride, bitmapProperties); } }
/// <summary> /// Creates a Direct2D bitmap from a GDI bitmap for the specified rendering target /// </summary> /// <param name="bitmap">Original bitmap</param> /// <param name="renderTarget">Destination rendering target</param> /// <param name="disposeOriginal">Whether or not to release the resources of the GDI bitmap</param> /// <returns>A Direct2D bitmap instance the caller is responsible for disposal</returns> public static Bitmap ToDirect2DBitmap( this System.Drawing.Bitmap bitmap, RenderTarget renderTarget, bool disposeOriginal = true) { // lock bits from the original bitmap so we can read its data BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb); // copy GDI bitmap data to Direct2D one var stream = new DataStream(data.Scan0, data.Stride * data.Height, true, false); var format = new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied); var props = new BitmapProperties(format); // create Direct2D bitmap and release resources var direct2DBitmap = new Bitmap(renderTarget, new Size2(bitmap.Width, bitmap.Height), stream, data.Stride, props); // release resources stream.Dispose(); bitmap.UnlockBits(data); if (disposeOriginal) { bitmap.Dispose(); } // return new bitmap return(direct2DBitmap); }
public Bitmap CreateBitmapFromResource(string name) { var assembly = Assembly.GetExecutingAssembly(); bool needsScaling = false; System.Drawing.Bitmap bmp; if (Direct2DTheme.MainWindowScaling == 1.5f && assembly.GetManifestResourceInfo($"FamiStudio.Resources.{name}@15x.png") != null) { bmp = System.Drawing.Image.FromStream(assembly.GetManifestResourceStream($"FamiStudio.Resources.{name}@15x.png")) as System.Drawing.Bitmap; } else if (Direct2DTheme.MainWindowScaling > 1.0f && assembly.GetManifestResourceInfo($"FamiStudio.Resources.{name}@2x.png") != null) { bmp = System.Drawing.Image.FromStream(assembly.GetManifestResourceStream($"FamiStudio.Resources.{name}@2x.png")) as System.Drawing.Bitmap; needsScaling = Direct2DTheme.MainWindowScaling != 2.0f; } else { bmp = System.Drawing.Image.FromStream(assembly.GetManifestResourceStream($"FamiStudio.Resources.{name}.png")) as System.Drawing.Bitmap; } // Pre-resize all images so we dont have to deal with scaling later. if (needsScaling) { var newWidth = (int)(bmp.Width * (Direct2DTheme.MainWindowScaling / 2.0f)); var newHeight = (int)(bmp.Height * (Direct2DTheme.MainWindowScaling / 2.0f)); bmp = new System.Drawing.Bitmap(bmp, newWidth, newHeight); } var bmpData = bmp.LockBits( new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); var stream = new DataStream(bmpData.Scan0, bmpData.Stride * bmpData.Height, true, false); var pFormat = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied); var bmpProps = new BitmapProperties(pFormat); var result = new Bitmap( renderTarget, new Size2(bmp.Width, bmp.Height), stream, bmpData.Stride, bmpProps); bmp.UnlockBits(bmpData); stream.Dispose(); bmp.Dispose(); return(result); }
public RenderScene(Control container, vetor2 locationScene, Size szScene) { this.sizeScene = szScene; this.container = container; this.locationScene = locationScene; this.imagesD2D = new List <MyImage>(); this.locations = new List <MATRIZES.vetor2>(); this.opacities = new List <float>(); this.sceneBitmapGDI = new System.Drawing.Bitmap(szScene.Width, szScene.Height); // cria uma imagem GDI da cena. } // MySceneImage()
private static Bitmap LoadBitmap(SharpDX.Direct2D1.RenderTarget target, System.Drawing.Bitmap bitmap) { var format = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied); var bmpProps = new BitmapProperties(format); var bmp = new Bitmap(target, new Size2(bitmap.Width, bitmap.Height), bmpProps); var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); bmp.CopyFromMemory(bitmapData.Scan0, bitmapData.Stride); bitmap.UnlockBits(bitmapData); return(bmp); }
public MyImage(System.Drawing.Bitmap image, Control container, Size size) { if ((container == null) || (image == null)) { return; } this.imageGDI = new System.Drawing.Bitmap(image, size); this.idRender = container.Handle; this.size = new SizeF(size.Width, size.Height); MyDevice2D.Instance().RegisterRenderTarget(container); this.imageDirect2D = GetDirect2DBitmapWithSharpDX(MyDevice2D.render[container.Handle], this.imageGDI); this.idRender = container.Handle; } // MyImage()
} // Rotate() private System.Drawing.Bitmap RotateImage(System.Drawing.Bitmap bmp, float angle) { System.Drawing.Bitmap rotatedImage = new System.Drawing.Bitmap(bmp.Width, bmp.Height); rotatedImage.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution); using (Graphics g = Graphics.FromImage(rotatedImage)) { // Set the rotation point to the center in the matrix g.TranslateTransform(bmp.Width / 2, bmp.Height / 2); // Rotate g.RotateTransform(angle); // Restore rotation point in the matrix g.TranslateTransform(-bmp.Width / 2, -bmp.Height / 2); // Draw the image on the bitmap g.DrawImage(bmp, new Point(0, 0)); } return(rotatedImage); } // RotateImage()
internal static Bitmap ToSharpDXBitmap(RenderTarget rt, System.Drawing.Bitmap image, float symbolScale) { if (image == null) { throw new ArgumentNullException("image"); } if (image.PixelFormat != GdiPixelFormat.Format32bppPArgb) { return(null); } var imageData = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat); var dataStream = new DataStream(imageData.Scan0, imageData.Stride * imageData.Height, true, false); var properties = new BitmapProperties { PixelFormat = new SharpDX.Direct2D1.PixelFormat { Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode = AlphaMode.Premultiplied } }; // ToDo apply scaling here! //var scaler = new BitmapScaler(rt.Factory.NativePointer); //scaler. //Load the image from the gdi resource var result = new Bitmap(rt, new Size2(image.Width, image.Height), dataStream, imageData.Stride, properties); image.UnlockBits(imageData); return(result); }
} // RemoveImage() // desenha numa só imagem, todas imagens registradas. Os dados precisam ser inseridos a cada renderização de frame. // Não apaga o frame de renderização anterior. public void DrawScene() { this.sceneBitmapGDI = new System.Drawing.Bitmap(sizeScene.Width, sizeScene.Height); // inicializa a imagem total da cena a renderizar. Graphics g = Graphics.FromImage(sceneBitmapGDI); // prepara para desenhar imagens GDI para dentro da imagem GDI da cena. for (int x = 0; x < this.imagesD2D.Count; x++) { // desenha a imagem GDI currente para dentro da imagem GDI da cena. g.DrawImageUnscaled(this.imagesD2D[x].GetGDIBitmap(), new Point((int)this.locations[x].X, (int)this.locations[x].Y)); } // for x this.sceneImageD2D = new MyImage(sceneBitmapGDI, container, sizeScene); // obtem a imagem Direct2D da cena inteira. // desenha a cena inteira, todas imagens foram resumidas em uma só imagem D2D. sceneImageD2D.Begin(); sceneImageD2D.Draw(locationScene); // desenha o frame de renderização. sceneImageD2D.End(); // limpa as listas, para construir uma nova cena de buffer. this.imagesD2D.Clear(); this.locations.Clear(); this.sceneImageD2D.Dispose(); g.Dispose(); } // DrawScene()
} // Clear() /// <summary> /// rotaciona a imagem em um incremento de ângulo, em graus. /// </summary> /// <param name="incAngulo">incremento de ângulo para rotacionar.</param> public void Rotate(double incAngulo) { this.imageGDI = RotateImage(this.imageGDI, (float)incAngulo); this.imageDirect2D = GetDirect2DBitmapWithSharpDX(MyDevice2D.render[this.idRender], this.imageGDI); } // Rotate()
public static D2DBitmap LoadBitmap(RenderContext context, System.Drawing.Bitmap bitmap) { var bmp = LoadBitmap(context.RenderTarget.DeviceContext2D, bitmap); return(new D2DBitmap(bmp)); }
public static D2DImageStrip2D LoadImageStrip2D(RenderContext context, System.Drawing.Bitmap bitmap, float unitWidth, float unitHeight, int count, int arrayCount, ImageStripOrientation orientation) { var bmp = LoadBitmap(context.RenderTarget.DeviceContext2D, bitmap); return(new D2DImageStrip2D(bmp, unitWidth, unitHeight, count, arrayCount, orientation)); }
public static D2DImageStrip LoadImageStrip(RenderContext context, System.Drawing.Bitmap bitmap, int count, ImageStripOrientation orientation) { var bmp = LoadBitmap(context.RenderTarget.DeviceContext2D, bitmap); return(new D2DImageStrip(bmp, count, orientation)); }