public static Texture2D BitmapToTexture(GraphicsDevice device, Gdi.Bitmap bitmap) { MemoryStream ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Png); // Save the bitmap to memory bitmap.Dispose(); // Dispose the bitmap object Texture2D tex = Texture2D.FromStream(device, ms); // Load the texture from the bitmap in memory ms.Close(); // Close the memorystream ms.Dispose(); // Dispose the memorystream return tex; // return the texture }
public static Texture2D ToTexture(drawing.Bitmap bitmap, GraphicsDevice graphicsDevice) { Texture2D texture; if (bitmap == null) { return null; } texture = new Texture2D(graphicsDevice, bitmap.Width, bitmap.Height); // MemoryStream to store the bitmap data. MemoryStream ms = new MemoryStream(); // Save image to MemoryStream bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); //Go to the beginning of the memory stream. ms.Seek(0, SeekOrigin.Begin); //Fill the texture. texture = Texture2D.FromStream(graphicsDevice, ms); ms.Close(); ms.Dispose(); ms = null; return texture; }
/// ------------------------------------------------------------------- ///<summary></summary> /// ------------------------------------------------------------------- void TS_SaveBitmap(Drawing.Bitmap bm) { string path = System.IO.Directory.GetCurrentDirectory() + @"\AutomationElementTest.bmp"; Comment("Saving image to " + path); bm.Save(path); TestObject.PSFileList.Add(path); m_TestStep++; }
public override void Render(GDI.Graphics g, Map map) { if (map.Center == null) throw (new ApplicationException("Cannot render map. View center not specified")); g.SmoothingMode = SmoothingMode; var envelope = ToSource(map.Envelope); //View to render if (DataSource == null) throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'")); // Get the transform var transform = new Matrix3x2(g.Transform.Elements); // Save state of the graphics object var gs = g.Save(); // Create and prepare the render target var rt = RenderTargetFactory.Create(_d2d1Factory, g, map); // Set anti-alias mode and transform rt.AntialiasMode = AntialiasMode; rt.Transform = transform; if (Theme != null) RenderInternal(_d2d1Factory, rt, map, envelope, Theme); else RenderInternal(_d2d1Factory, rt, map, envelope); // Clean up the render target RenderTargetFactory.CleanUp(rt, g, map); // Restore the graphics object g.Restore(gs); // Invoke LayerRendered event OnLayerRendered(g); }
/// <summary> /// Converts a <see cref="d.Image"/> to a byte array. /// </summary> /// <param name="image">The image to convert</param> /// <param name="format">The format in which to save the image</param> /// <returns>A byte array representing the image</returns> private byte[] ConvertToByte(d.Image image, ImageFormat format) { byte[] result; using (var stream = new System.IO.MemoryStream()) { image.Save(stream, format); result = stream.ToArray(); } return result; }
private BitmapImage BitmapToBitmapImage(Drawing.Bitmap bitmap) { BitmapImage resultImage; using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Bmp); stream.Position = 0; resultImage = new BitmapImage(); resultImage.BeginInit(); resultImage.StreamSource = stream; resultImage.CacheOption = BitmapCacheOption.OnLoad; resultImage.EndInit(); } return resultImage; }