SlimDX rendering a visual to a bitmap - wpf

I want to render a viewport3d visual to an image using hardware rendering. How could I do that using SlimDX? I'm using WPF & VB.NET 4.0 and SlimDX. Any tips will help, thanks in advance. :)
Here is the code I use to render a viewport3d, it is very slow and gives bad results:
//rendering viewport3D to image
Dim viewportPlate As New RenderTargetBitmap(600, 600, 96 , 96 , PixelFormats.Pbgra32)
viewportPlate.Render(viewport3d)
//path to save
Dim path As String = imgSave
Dim fs As FileStream = New FileStream(path, FileMode.Create)
//encoding to PNG and saving
Dim encoder As BitmapEncoder = New PngBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(viewportPlate))
encoder.Save(fs)
PS: If anyone has a better idea on how to do this using something other than SlimDX I will welcome those too...

Related

VB.Net WPF : Define BitmapImage with Resources

I'm trying to use a RadDesktopAlert from Telerik and I need to set an Icon to it.
So, I did this :
Dim bi As New BitmapImage
bi.BeginInit()
bi.UriSource = New Uri("pack://application:,,,/Resources/questions1.png")
bi.EndInit()
alert.Icon = bi
alert.IconColumnWidth = 75
But my alert don't have any icon, it sounds like that it can't find the resource... Build action is set to "Resource" for my file.
Can I have help please ?
Thanks !
This should work, provided that the image file is in folder "Resources" in your Visual Studio project, and that its Build Action is set to Resource:
Dim bitmap As New BitmapImage(New Uri("pack://application:,,,/Resources/questions1.png"))
Dim image As New Image() With {.Source = bitmap}
alert.Icon = image

I need help converting System.Window.Size to System.Window.Point

I just started working on an existing VB project where the end user wants the ability to print a WPF window as a full page to a printer. I found this code sample in C# and it worked just fine in C#
Printing WPF Window to Printer and Fit on a Page
However when I tried converting it to VB I am getting two errors
System.Drawing.Size cannot be converted to System.Windows.Size
System.Drawing.Point cannot be converted to System.Windows.Point
I sort of know what the difference in Drawing.Size and Windows.Size is based on this (and a couple of other) SO threads What is the difference between System.Drawing.Point and System.Windows.Point? but I cannot figure out how to modify my translation in a way that I can make the conversion happen in VB the way it works on C#. The errors appear on the Measure(sz) and the Arrange(new Rect(.....) lines.
What do I need to do to make this work?
Private Sub PrintWindow()
Dim printDlg As PrintDialog = New PrintDialog()
If printDlg.ShowDialog() = True Then
Dim capabilities As System.Printing.PrintCapabilities =
printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket)
Dim scale As Double = Math.Min(capabilities.PageImageableArea.ExtentWidth / ActualWidth,
capabilities.PageImageableArea.ExtentHeight / ActualHeight)
LayoutTransform = New ScaleTransform(scale, scale)
Dim sz As New Size(CInt(capabilities.PageImageableArea.ExtentWidth),
CInt(capabilities.PageImageableArea.ExtentHeight))
Measure(sz)
Arrange(New Rect(New Point(CInt(capabilities.PageImageableArea.OriginWidth),
CInt(capabilities.PageImageableArea.OriginHeight)), sz))
printDlg.PrintVisual(Me, "First Fit to Page WPF Print")
End If
End Sub
Why not do something like this ?
Dim sz As New System.Windows.Size(CInt(capabilities.PageImageableArea.ExtentWidth),
CInt(capabilities.PageImageableArea.ExtentHeight))
Arrange(New Rect(New System.Windows.Point(CInt(capabilities.PageImageableArea.OriginWidth),
CInt(capabilities.PageImageableArea.OriginHeight)), sz))

WPF loading image from internet

Hey all i am trying to find a way to load an image via the WebClient using a WFP window.... The code below loads just fine when i am using a normal Windows Form:
Dim wc As New Net.WebClient
picNextTopic1.Image = Image.FromStream(wc.OpenRead(theAPI.ImgURL(2).Replace("{width}", "50").Replace("{height}", "50")))
However, this doesnt seem to work for WPF???
The WPF seems to use a .source for images? How can i convert that code above in order to use it with a WPF window?
WPF is not Windows Forms. You need to build WPF applications completely different. Properties, INotifyPropertyChanged etc.
But to your problem:
Dim wc As New WebClient()
Dim bytes = wc.DownloadData("http://....")
Dim ms = New MemoryStream(bytes)
Dim img = New BitmapImage()
ms.Seek(0, SeekOrigin.Begin)
img.BeginInit()
img.StreamSource = ms
img.EndInit()
picNextTopic1.Source = img

Printing in WPF

I've created some windows in wpf that I need to print to one xps document. Each window opens, loads the relevant data and then immediately closes. Currently I use the below code to create the xps:
Using doc = New XpsDocument(TempLoc, FileAccess.Write)
Dim writer = XpsDocument.CreateXpsDocumentWriter(doc)
Dim collator = writer.CreateVisualsCollator()
Dim Window1 As Window1 = New Window1()
Window1.ShowDialog()
Dim Window2 As Window2 = New Window2()
Window2.ShowDialog()
Dim WindowX As WindowX = New WindowX()
WindowX.ShowDialog()
collator.BeginBatchWrite()
collator.Write(Window1)
collator.Write(Window2)
collator.Write(WindowX)
collator.EndBatchWrite()
End Using
Dim doc2 = New XpsDocument(TempLoc, FileAccess.Read)
Dim seq = doc2.GetFixedDocumentSequence()
Dim window = New Window()
window.Content = New DocumentViewer() With {.Document = seq}
window.ShowDialog()
doc2.Close()
However the trouble with this approach is that the area printed varies between machines - I assume this is due to the local screen size being used etc.
Is it possible to make the program print the full window independent of the computer its on by modifying this code? Alternativly is there a better way to approach this problem?
Thanks for any help
I print with FixedDocuments by either appending or adding placed UIElements. I posted the full source code to my helper class here. You may find that breaking it down by UIElements gives you much better control over your exact printed output, though yes, it requires you to separate printing code instead of just emitting your window.
I've used this helper class to create some very nicely formated multi-page reports that always come out the same on every computer.

How can I save the picture on image control in wpf?

I have a simple wpf application WIA.My app has an image control...
I was wondering how can I save the scanned picture on my hard disk?
Depends on the type of the Image.Source, assuming that you have a BitmapSource as in the article it should be along those lines:
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)image.Source));
using (FileStream stream = new FileStream(filePath, FileMode.Create))
encoder.Save(stream);
If it's a System.Drawing.Image object then just call Save?

Resources