c# - Using WPF on a DLL to create Images Dynamically (instead of GDI+) -


I need to generate an image dynamically, and after reading the tutorial I know that I have all the controls And I can use the layout to generate my rendering, and then save it as a JPG. The idea is to use it instead of GDI +, which is quite primitive.

The question is, how do I create a regular DLL file that produces a WPF canvas programmatically, then I can control it and then eject it in an image file. Keep in mind that it will be used by ASP.NET applications.

Any idea anyone?

The beginning of that example is good but I have found out that it has got very unnecessary junk . The main thing is that you do not need a separate WPF project.

Here's what to do:

  • Presentation in the web project, Presentation Framework and Windowsbase.
  • Create canvas and other WPF objects programmatically in a CATA thread.
  • Make sure they call some specific methods to update outside the context of a WPF app.
  • Assign them with an image with RenderTargetBitmap.
  • Close the thread's dispatcher.
  • Set the mime type and output the image with ASP.NET.

In order to make it more efficient, you can use the same thread again, instead of creating a new one for each image. In that case you need to clear the dispatcher when you close the thread.

Here I have the full working code:

  using the system; Using System.Web; Using System.Threading; Using System.IO; Using System.Windows; System.Windows.Media; System.Windows.Media Using imaging; Using System.Windows.Controls; Using System.Windows.Documents; Public Partial Class _Default: System.Web.UI.Page {Private Byte] imageBuffer; Public Zero Page_load (Object Sender, EventAgds E) {this.RenderImage (); Response.Clear (); Response.ContentType = @ "Image / PNG"; Response.BufferOutput = True; Response.BinaryWrite (this.imageBuffer); Response.Flush (); } Public Zero Rider Image () {Thread Worker = New Thread (New Thread Start (This. Reader Image Worker)); Worker.SetApartmentState (ApartmentState.STA); Worker.Name = "Reader Image Worker"; Worker.Start (); Worker.Join (); } Public Zero RenderImageWorker () {Canvas image canvas = new canvas {width = 600, height = 200, background = brush. Ezur}; TextBlock tb = new text block (); Tb.Width = (double) 400; //tb.Height = (double) 200; Tb.TextAlignment = TextAlignment.Center; Tb.Inlines.Add (new run ("this")); Tb.Inlines.Add (new bold (new run ("bold"); Tb.Inlines.Add (new part ("text."); Tb.FontSize = 30; Tb.Foreground = brush.blue; imageCanvas Children.Add (TB); // Update Layout Image Sheeting. Mair (new size (imageCanvas.Width, imageCanvas.Height); ImageCanvas.Arrange (new size (new size (imageCanvas.Width, imageCanvas.Height)); RenderTargetBitmap bitmapRenderer = New RenderTargetBitmap ((int) imageCanvas.ActualWidth, (int) imageCanvas.ActualHeight, 96, 96, PixelFormats.Pbgra32); bitmapRenderer.Render (imageCanvas); PngBitmapEncoder png = New PngBitmapEncoder (); png.frames.Add BitmapFrame.Create (bitmapRenderer); (using memorystream memorystream = new memorystream ()) {png.Save (memoryStream ); This.imageBuffer = memoryStream.ToArray ();} if (bitmap renderer.dispatchor.thread.islivee) {bitmap renderer.dispactor, invoke shutdown ();}}}  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -