WPF issue with VisualBrush for Image while moving - wpf

I have Image and i have created visual brush for image when i move object form one point to another. but I don't see image on visual brush. if you see my rectangle, it suppose to show image.
See image : http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e8833983-3d73-45e1-8af1-3bc27846441d
here is code:
internal static VisualBrush GetVisualBrushByObject(LabelObject obj, Rect objectRect, int quality, FlowDirection flowdirection)
{
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
Rect objectbounds = new Rect(0, 0, objectRect.Width, objectRect.Height);
if (obj is TextObject)
{
TextObject txtObj = obj.Clone() as TextObject;
DymoTextBlock txtBlock = txtObj.DymoTextBlock as DymoTextBlock;
objectbounds = new Rect(txtBlock.GetNaturalSize(txtBlock.GetFormattedText(flowdirection)));
}
if (obj is ImageObject)
{
drawingContext.DrawImage(((ImageObject)obj).Image, objectbounds);
}
LabelObject.RenderParams lrp = new LabelObject.RenderParams(drawingContext, new Common.Resolution(96, 96), false, objectbounds, flowdirection);
obj.Render(lrp);
VisualBrush vBrush = new VisualBrush();
vBrush.TileMode = TileMode.None;
vBrush.Stretch = Stretch.Fill;
if (obj is ImageObject)
{
vBrush.Opacity = 0.4;
}
drawingContext.Close();
vBrush.Visual = drawingVisual;
return vBrush;
}
pls help me
Thanks

If you are moving your image with Transforms(TranslateTransform), then you have to undo it for the visual brush phase.

Related

WPF Including Tooltip in Render with RenderTargetBitmap

I've searched and searched but haven't been able to find anything with the same problem as me. I'm trying to render some high resolution/dpi screenshots of a WPF application. Only problem is that I need to include information from chart tooltips in the render, other than that I can save screenshots just fine.
I'm currently using Infragistics XamDataChart and I generate the tooltips in code rather xaml.
Anyone have a clue how to get the tooltip in the visual tree so it renders? Or to be able to render the whole window and everything inside of it including tooltip overlays?
Code for the render:
public static void RenderVisualToFile(this FrameworkElement visual)
{
var width = (int)visual.RenderSize.Width;
var height = (int)visual.RenderSize.Height;
RenderTargetBitmap renderTarget = new RenderTargetBitmap(width * 4, height * 4, 384, 384, PixelFormats.Pbgra32);
renderTarget.Render(visual);
// Encode and save to PNG file
var enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(renderTarget));
if (Directory.Exists("Screenshots"))
{
using (var stm = File.Create(#"Screenshots\Render_" + DateTime.Now.ToString("yyMMMdd_HHmmss") + ".png"))
enc.Save(stm);
}
else
{
Directory.CreateDirectory("Screenshots");
using (var stm = File.Create(#"Screenshots\Render_" + DateTime.Now.ToString("yyMMMdd_HHmmss") + ".png"))
enc.Save(stm);
}
}
And I call this in the MainWindow code behind.
if (e.Key == Key.PrintScreen)
{
this.RenderVisualToFile();
}
It is a bit late but maybe someone can use my solution.
My Screenshot class is based on the following solutions:
The creation of the screenshots is based on a post on the QMINO technical blog High Res Screenshots
To get the currently opened ToolTips I use the code fragment posted here: Find all opened Popups in WPF application
It took me a while to find a solution on how to merge the UIElement for the screenshot with the PopUps but a comment of Clemens gave the final clue Overlay Images with PngBitmapEncoder in WPF
I use tuples to to return multiple parameters C# 7.0 Tuples.
So here is my class:
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
public class Screenshot
{
//UIElement to create screenshot
private UIElement _element;
//Bounds for the screenshot
private Rect _screenshotBounds;
//Path for Screenshot
private string _path;
private const int DPI = 384;
private const double BASEDPI = 96;
private const double DPISCALE = DPI / BASISDPI;
public Screenshot(UIElement element, string path)
{
this._element = element;
this._screenshotBounds = this.createBounds(this._element);
this._path = path;
}
//public interface to create the screenshot
public void createScreenshot()
{
if (this._element == null)
{
return;
}
//Create a list of tuples with the elements to render in the screenshot
List<(UIElement, Rect, Point)> listElements = new List<(UIElement, Rect, Point)>
{
//Fist element in the list should be the actual UIElement
this.createElementBoundPosition(this._element);
};
RenderTargetBitmap renderBitMap = this.createBitMap(this._screenshotBounds);
//Get the opened Popups, create a list of tuples for the Popups and add them to the list of elements to render
listElements.AddRange(this.createListPopUpBoundsPosition( this.getOpenPopups()));
DrawingVisual drawingVisual = this.createDrawingVisual(listElements);
renderBitMap.Render(drawingVisual);
this.saveRTBAsPNG(renderBitMap);
}
//Create DrawingVisual based on List of Tuples
private DrawingVisual createDrawingVisual(List<(UIElement, Rect, Point)> listElements)
{
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext context = drawingVisual.RenderOpen())
{
foreach((UIElement element, Rect bounds, Point position) in listElements)
{
VisualBrush visualBrush = new VisualBrush(element);
context.DrawRectangle(visualBrush, null, new Rect(position, bounds.Size));
}
}
return drawingVisual;
}
//Save RenderTargetBitmap to file
private void saveRTBAsPNG(RenderTargetBitmap bitmap)
{
PngBitmapEncoder pngBitmapEncoder = new PngBitmapEncoder()
{
Interlace = PngInterlaceOption.On
}
pngBitmapEncoder.Frames.Add(BitmapFrame.Create(bitmap));
using (FileStream fileStream = File.Create(this._path))
{
pngBitmapEncoder.Save(fileStream);
}
}
//Create Bounds for Element
private Rect createBounds(UIElement element)
{
new Rect(new Size((int)element.RenderSize.Width, (int)element.RenderSize.Height));
}
//Create a Tuple with the Element, its bounds and its position
private (UIElement element, Rect bounds, Point position) createElementBoundPosition(UIElement element)
{
return (element, this.createBounds(element), element.PointToScreen(new Point(0,0)));
}
//create the RenderTargetBitmap
private RenderTargetBitmap createBitMap(Rect bounds)
{
(int width, int height) calculatedBounds = this.calculateBounds(bounds);
return new RenderTargetBitmap(calculatedBounds.width, calculatedBounds.height, DPI, DPI, PixelFormats.Pbgra32);
}
//recalculate bounds according to the scale
private (int width, int heigth) calculateBounds(Rect bounds)
{
int width = (int)(bounds.Width * DPISCALE);
int height = (int)(bounds.Height * DPISCALE);
return (width, height);
}
//Convert the list of Popups into a List of Tuples
private List<(UIElement element, Rect bounds, Point position)> createListPopUpBoundsPosition(List<Popup> listPopup)
{
List<(UIElement, Rect, Point)> list = new List<(UIElement, Rect, Point)>();
foreach (Popup p in listPopup)
{
//The Child-Element contains the UIElement to render
UIElement uiElement = p.Child;
list.Add(this.createElementBoundPosition(uiElement));
}
return list;
}
//get the open Popups
private List<Popup> getOpenPopups()
{
return PresentationSource.CurrentSources.OfType<HwndSource>()
.Select(h => h.RootVisual)
.OfType<FrameworkElement>()
.Select(f => f.Parent)
.OfType<Popup>()
.Where(p => p.IsOpen).ToList();
}
}

Hex PathGeometry fill instead of drawing border only

I have a descendent of FrameworkElement with the following methods:
class HexVisual : FrameworkElement {
/* ... */
private PathGeometry GetHexGeometry(Hexagon Hexagon) {
var geometry = default(PathGeometry);
if(mHexGeometries.TryGetValue(Hexagon, out geometry)){
return geometry;
}
PathFigure figure = new PathFigure();
int i = 0;
foreach (Point point in Hexagon.Polygon.Points) {
if (i == 0) {
figure.StartPoint = point;
i++;
continue;
}
figure.Segments.Add(new LineSegment(point, true));
}
geometry = new PathGeometry();
geometry.Figures.Add(figure);
geometry.FillRule = FillRule.Nonzero;
return geometry;
}
private DrawingVisual CreateHexVisual() {
DrawingVisual visual = new DrawingVisual();
using (DrawingContext context = visual.RenderOpen()) {
context.DrawGeometry(BorderBrush, BorderPen, mHexGeometry);
}
return visual;
}
}
When I add this visual to the Canvas, I am getting a filled hexagon rather than just the stroke (border).
What am I doing wrong here that is causing the geometry to fill?
Not sure if I really understand your question, because you can't "add visuals to a Canvas", but maybe you just set BorderBrush to null or Transparent, or replace
context.DrawGeometry(BorderBrush, BorderPen, mHexGeometry);
with
context.DrawGeometry(null, BorderPen, mHexGeometry);

Designing PDF with HTML in WPF application

Is it possible to design a pdf using HTML and js in a WPF application(C#). I tried using iTextSharp by taking a image of the control i want to export and then pasted in the pdf. It worked, but the problem is the control has to be visible. So, lets say if there is a listview in that control and that listview doesn't fits in one window, then whole listview doesn't gets exported. Any ideas how to approach?
This is so far I've done for exporting ListView to pdf.
ListView item = list;
double width = item.ActualWidth;
double height = item.ActualHeight;
Document doc = new Document(new iTextSharp.text.Rectangle(1200f, 700f));
String filePath;
string path = Environment.CurrentDirectory + "\\export\\" ;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
filePath = path + "\\Test_.pdf";
PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Append));
doc.Open();
RenderTargetBitmap bmpCopied = new RenderTargetBitmap((int)Math.Round(width + 100), (int)Math.Round(height + 50), 0, 0, PixelFormats.Default);
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
item.Background = Brushes.White;
VisualBrush visualBrush = new VisualBrush(item);
drawingContext.DrawRectangle(Brushes.White, null, new Rect(new Point(), new Size(width + 100, height + 50)));
drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(), new Size(width + 100, height + 50)));
}
bmpCopied.Render(drawingVisual);
item.Background = Brushes.Transparent;
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmpCopied));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(data);
doc.Add(pdfImage);
doc.Close();
The pdf gets created, but opening the pdf shows error. Secondly, my ListView is scrollable, will all the items be shown in the pdf?. I just need to get it working. Any way would be ok for me

Snapshot of an WPF Canvas Area using RenderTargetBitmap

I want to create a Snapshot of the Canvas Area in my Application. I'm using Visual brush to get the Snapshot and saving the same using PngEncoder. But the resulting PNG is just a empty black image. I'm not sure the issue is with the BitmapSource created or the PNGEncoder issue. Here is the code I'm using to obtain the same.
public void ConvertToBitmapSource(UIElement element)
{
var target = new RenderTargetBitmap((int)(element.RenderSize.Width), (int)(element.RenderSize.Height), 96, 96, PixelFormats.Pbgra32);
var brush = new VisualBrush(element);
var visual = new DrawingVisual();
var drawingContext = visual.RenderOpen();
drawingContext.DrawRectangle(brush, null, new Rect(new Point(0, 0),
new Point(element.RenderSize.Width, element.RenderSize.Height)));
drawingContext.Close();
target.Render(visual);
PngBitmapEncoder encoder = new PngBitmapEncoder();
BitmapFrame outputFrame = BitmapFrame.Create(target);
encoder.Frames.Add(outputFrame);
using (FileStream file = File.OpenWrite("TestImage.png"))
{
encoder.Save(file);
}
}
Not sure why exactly your code isn't working. This works:
public void WriteToPng(UIElement element, string filename)
{
var rect = new Rect(element.RenderSize);
var visual = new DrawingVisual();
using (var dc = visual.RenderOpen())
{
dc.DrawRectangle(new VisualBrush(element), null, rect);
}
var bitmap = new RenderTargetBitmap(
(int)rect.Width, (int)rect.Height, 96, 96, PixelFormats.Default);
bitmap.Render(visual);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using (var file = File.OpenWrite(filename))
{
encoder.Save(file);
}
}
Thank you both for the question and the answer.
For the benefit of the others looking for the same answer
I found that Clemens way leaves a black band in the image with the image shifted either down or right. As if it was not rendering the element at the correct position in the bitmap.
So I had to use the VisualBrush as Amar suggested.
Here is the code that worked for me:
RenderTargetBitmap RenderVisual(UIElement elt)
{
PresentationSource source = PresentationSource.FromVisual(elt);
RenderTargetBitmap rtb = new RenderTargetBitmap((int)elt.RenderSize.Width,
(int)elt.RenderSize.Height, 96, 96, PixelFormats.Default);
VisualBrush sourceBrush = new VisualBrush(elt);
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
using (drawingContext)
{
drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0),
new Point(elt.RenderSize.Width, elt.RenderSize.Height)));
}
rtb.Render(drawingVisual);
return rtb;
}

Merge XPS landscape orientation

I've implemented this solution and it worked for me:
Can multiple xps documents be merged to one in WPF?
My problem is that the pages I want to merge are in landscape orientation. When the ContainerVisual is added it creates by default a page in vertical orientation. How can I change the orientation to ContainerVisual?
private void AddXPSDocument(string sourceDocument, SerializerWriterCollator vxpsd)
{
XpsDocument xpsOld = new XpsDocument(sourceDocument, FileAccess.Read);
FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();
foreach (DocumentReference r in seqOld.References)
{
FixedDocument d = r.GetDocument(false);
foreach (PageContent pc in d.Pages)
{
FixedPage fixedPage = pc.GetPageRoot(false);
double width = fixedPage.Width;
double height = fixedPage.Height;
Size sz = new Size(width, height);
fixedPage.Width = width;
fixedPage.Height = height;
fixedPage.Measure(sz);
fixedPage.Arrange(new Rect(new Point(), sz));
//fixedPage.UpdateLayout();
ContainerVisual newPage = new ContainerVisual();
newPage.Children.Add(fixedPage);
vxpsd.Write(newPage);
}
}
xpsOld.Close();
}
You need to add a RotateTransform to the page visual.
Visual originalPage = Paginator.GetPage(pageNumber).Visual;
var pageContentVisual = new ContainerVisual();
TransformGroup group = new TransformGroup();
group.Children.Add(new RotateTransform { Angle = 90.0 });
pageContentVisual.Transform = group;
pageContentVisual.Children.Add(originalPage);
Note: The above was copied from a custom DocumentPaginator however you should be able to apply it your situation.

Resources