Force render Viewbox in order to draw it on page - c#

I'm using a FlowDocument to create a report. Now, I created a paginator in order to be able to repeat the header on every page, however, it does not get rendered on each page. I suspect the problem is that the Viewbox is not rendered/created untill you try to display it.
This is my GetPage method:
public override DocumentPage GetPage(int pageNumber) {
DocumentPage page = m_Paginator.GetPage(pageNumber);
ContainerVisual newpage = new ContainerVisual();
DrawingVisual title = new DrawingVisual();
using (DrawingContext ctx = title.RenderOpen())
{
var header = getHeader();
RenderTargetBitmap bmp = new RenderTargetBitmap(165, 32, 96, 96,
PixelFormats.Pbgra32);
bmp.Render(header);
ctx.DrawImage(bmp,new Rect(new Point(0,0),new Size(166, 33)));
}
ContainerVisual smallerPage = new ContainerVisual();
title.Children.Add(getHeader());
newpage.Children.Add(title);
smallerPage.Children.Add(page.Visual);
smallerPage.Transform = new MatrixTransform(0.95, 0, 0, 0.95, 0.025 * page.ContentBox.Width, 0.025 * page.ContentBox.Height);
newpage.Children.Add(smallerPage);
newpage.Transform = new TranslateTransform(m_Margin.Width, m_Margin.Height);
return new DocumentPage(newpage, m_PageSize, Move(page.BleedBox), Move(page.ContentBox));
}
Here's the Move method:
Rect Move(Rect rect) {
if (rect.IsEmpty) {
return rect;
}
else {
return new Rect(rect.Left + m_Margin.Width, rect.Top + m_Margin.Height,
rect.Width, rect.Height);
}
}
And here is getHeader() (Yes, I know, it should be GetHeader() - them conventions)
private Viewbox getHeader() {
Grid gr = new Grid();
var sr = Application.GetResourceStream(new Uri("Propuestas;component/img/log.xaml", UriKind.Relative));
var img = (Canvas)XamlReader.Load(new XmlTextReader(sr.Stream));
var logo = new Viewbox {
Child = img,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
Width = 165
};
var detalles = new TextBlock {
FontSize = 10,
FontFamily = new FontFamily("Verdana"),
Padding = new Thickness(logo.Width + 15, 0, 0, 0)
};
App.Comando.CommandText = "SELECT RazEmp, DirEmp, CpEmp, PobEmp, ProEmp, TelEmp, CifEmp FROM META4.Empresa";
using (var reader = App.Comando.ExecuteReader())
while (reader.Read())
detalles.Text = "" + reader.GetString(0).Trim() + "\n" + reader.GetString(1).Trim() + "\n" +
reader.GetDecimal(2) + " - " + reader.GetString(3).Trim() + "(" +
reader.GetString(4).Trim() + ")\n" + "Tlf: " + reader.GetString(5).Trim() +
"\nCIF: " + reader.GetString(6).Trim();
var pd = new TextBox {
Text = "PEDIDO DE COMPRA",
TextAlignment = TextAlignment.Left,
FontSize = 19,
FontFamily = new FontFamily("Verdana"),
FontWeight = FontWeights.Bold,
Background = new SolidColorBrush(Color.FromRgb(192, 192, 192)),
Margin = new Thickness(logo.Width + 15, 10, 0, 20),
BorderThickness = new Thickness(0)
};
gr.ColumnDefinitions.Add(new ColumnDefinition());
gr.ColumnDefinitions.Add(new ColumnDefinition());
gr.RowDefinitions.Add(new RowDefinition());
gr.RowDefinitions.Add(new RowDefinition());
Grid.SetRow(logo, 0);
Grid.SetRow(detalles, 0);
Grid.SetRow(pd, 1);
Grid.SetColumn(pd, 0);
Grid.SetColumnSpan(pd, 2);
Grid.SetColumnSpan(detalles, 2);
gr.Children.Add(logo);
gr.Children.Add(detalles);
gr.Children.Add(pd);
Viewbox vb = new Viewbox();
vb.Child = gr;
return vb;
}
However, when I hit print, it prints it normally, without repeating the header. I can see the query running up in debug, so addHeader() gets executed. The width and height are predetermined and fixed. Both header.Width/header.Height and header.ActualWidth/header.ActualHeight give me either 0 or NaN, which makes me believe that the viewbox isn't rendered in the background. Is there any way I would be able to repeat this on each page?
The problem is that my header contains one image and two parts text. I already had it created to be put on the first page only, but now the requirements have changed and I have to repeat it on every page.
Any help is greatly appreciated.
Later Edit: Also tried this, didn't work either.
private static BitmapSource CaptureScreen(Visual target, double dpiX, double dpiY) {
if (target == null)
return null;
Size size = new Size(165, 31.536);
RenderTargetBitmap rtb = new RenderTargetBitmap((int)(size.Width * dpiX / 96.0),
(int)(size.Height * dpiY / 96.0),
dpiX,
dpiY,
PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen()) {
VisualBrush vb = new VisualBrush(target);
ctx.DrawRectangle(vb, null, new Rect(new Point(), size));
}
rtb.Render(dv);
return rtb;
}

Fixed by adding
vb.Measure(new System.Windows.Size(headerWidth, headerHeight));
vb.Arrange(new Rect(15,15,headerWidth,headerHeight));
vb.UpdateLayout();
to the getHeader method. Now it looks like this:
private Viewbox getHeader() {
Grid gr = new Grid();
var sr = Application.GetResourceStream(new Uri("Propuestas;component/img/log.xaml", UriKind.Relative));
var img = (Canvas)XamlReader.Load(new XmlTextReader(sr.Stream));
var logo = new Viewbox {
Child = img,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
Width = 165
};
var detalles = new TextBlock {
FontSize = 10,
FontFamily = new FontFamily("Verdana"),
Padding = new Thickness(logo.Width + 15, 0, 0, 0)
};
App.Comando.CommandText = "SELECT RazEmp, DirEmp, CpEmp, PobEmp, ProEmp, TelEmp, CifEmp FROM META4.Empresa";
using (var reader = App.Comando.ExecuteReader())
while (reader.Read())
detalles.Text = "" + reader.GetString(0).Trim() + "\n" + reader.GetString(1).Trim() + "\n" +
reader.GetDecimal(2) + " - " + reader.GetString(3).Trim() + "(" +
reader.GetString(4).Trim() + ")\n" + "Tlf: " + reader.GetString(5).Trim() +
"\nCIF: " + reader.GetString(6).Trim();
var pd = new TextBox {
Text = "PEDIDO DE COMPRA " + numPCO,
TextAlignment = TextAlignment.Left,
FontSize = 19,
FontFamily = new FontFamily("Verdana"),
FontWeight = FontWeights.Bold,
Background = new SolidColorBrush(Color.FromRgb(192, 192, 192)),
Margin = new Thickness(logo.Width + 15, 10, 0, 20),
BorderThickness = new Thickness(0)
};
gr.ColumnDefinitions.Add(new ColumnDefinition());
gr.ColumnDefinitions.Add(new ColumnDefinition());
gr.RowDefinitions.Add(new RowDefinition());
gr.RowDefinitions.Add(new RowDefinition());
Grid.SetRow(logo, 0);
Grid.SetRow(detalles, 0);
Grid.SetRow(pd, 1);
Grid.SetColumn(pd, 0);
Grid.SetColumnSpan(pd, 2);
Grid.SetColumnSpan(detalles, 2);
gr.Children.Add(logo);
gr.Children.Add(detalles);
gr.Children.Add(pd);
Viewbox vb = new Viewbox();
vb.Child = gr;
vb.Measure(new System.Windows.Size(headerWidth, headerHeight));
vb.Arrange(new Rect(15,15,headerWidth,headerHeight));
vb.UpdateLayout();
return vb;
}

Related

How to create a table with multiple columns in ios

I am a beginner in xamarin ios, I want to create a table with 3 columns and multiple rows, and the no of row is depends on the data coming from the API .How can I do this...
Please help me...
I want to create the table like this...
You can simulate drawing the chart by following steps.
Custom TableHeaderView with three black rectangle ,just need add three label with its black border.
Do the same work as step1 with TableViewCell, and then place a textfield in first rectangle, place a button and a label in second rectangle,place two button in third rectangle.
Sample Code
TableHeaderView
public class headerView : UIView
{
public headerView()
{
this.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 50);
nfloat width = UIScreen.MainScreen.Bounds.Width / 3;
UILabel title = new UILabel();
title.Text = "Title";
title.TextColor = UIColor.Black;
title.TextAlignment = UITextAlignment.Center;
title.Font = UIFont.SystemFontOfSize(20);
title.Layer.BorderColor = UIColor.Black.CGColor;
title.Layer.BorderWidth = 1;
title.Frame = new CGRect(0, 0, width, 50);
this.Add(title);
UILabel File = new UILabel();
File.Text = "File";
File.TextColor = UIColor.Black;
File.TextAlignment = UITextAlignment.Center;
File.Font = UIFont.SystemFontOfSize(20);
File.Layer.BorderColor = UIColor.Black.CGColor;
File.Layer.BorderWidth = 1;
File.Frame = new CGRect(width, 0, UIScreen.MainScreen.Bounds.Width / 3, 50);
this.Add(File);
UILabel Option = new UILabel();
Option.Text = "Option";
Option.TextColor = UIColor.Black;
Option.TextAlignment = UITextAlignment.Center;
Option.Font = UIFont.SystemFontOfSize(20);
Option.Layer.BorderColor = UIColor.Black.CGColor;
Option.Layer.BorderWidth = 1;
Option.Frame = new CGRect(width * 2, 0, UIScreen.MainScreen.Bounds.Width / 3, 50);
this.Add(Option);
}
}
table.TableHeaderView = new headerView();
TableViewCell
public CustomVegeCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId)
{
SelectionStyle = UITableViewCellSelectionStyle.Gray;
nfloat width = UIScreen.MainScreen.Bounds.Width / 3;
UIView headingLabel = new UIView();
headingLabel.Layer.BorderColor = UIColor.Black.CGColor;
headingLabel.Layer.BorderWidth = 1;
headingLabel.Frame = new CGRect(0, 0, width, 100);
UIView subheadingLabel = new UIView();
subheadingLabel.Layer.BorderColor = UIColor.Black.CGColor;
subheadingLabel.Layer.BorderWidth = 1;
subheadingLabel.Frame = new CGRect(width, 0, width, 100);
UIView imageView = new UIView();
imageView.Layer.BorderColor = UIColor.Black.CGColor;
imageView.Layer.BorderWidth = 1;
imageView.Frame = new CGRect(width *2, 0, width, 100);
ContentView.Add (headingLabel);
ContentView.Add (subheadingLabel);
ContentView.Add (imageView);
///add additional control here
}

How to apply more than one filter to the image using Aforge

I can filter red and blue color separately but i want to filter both at the same time.. so my code is like this
//FOR RED COLOR
ColorFiltering filter = new ColorFiltering();
filter.Red = new IntRange(100, 255);
filter.Green = new IntRange(0, 75);
filter.Blue = new IntRange(0, 75);
filter.ApplyInPlace(image1);
MyDraw(image1);
// FOR BLUE COLOR
EuclideanColorFiltering filter2 = new EuclideanColorFiltering();
filter2.CenterColor = new RGB(Color.FromArgb(9, 39, 101));
filter2.Radius = 50;
filter2.ApplyInPlace(image1);
MyDraw(image1);
public void MyDraw(Bitmap image)
{
BlobCounter blobCounter = new BlobCounter();
blobCounter.MinWidth = 2;
blobCounter.MinHeight = 2;
blobCounter.FilterBlobs = true;
blobCounter.ObjectsOrder = ObjectsOrder.Size;
Grayscale grayFilter = new Grayscale(0.2125, 0.7154, 0.0721);
Bitmap grayImage = grayFilter.Apply(image);
blobCounter.ProcessImage(grayImage);
Rectangle[] rects = blobCounter.GetObjectsRectangles();
foreach (Rectangle recs in rects)
{
if (rects.Length > 0)
{
Rectangle objectRect = rects[0];
//Graphics g = Graphics.FromImage(image);
Graphics g = pictureBox1.CreateGraphics();
reception = "Cam," + objectRect.X + "," + objectRect.Y;
Console.WriteLine("X: " + objectRect.X + " Y:" + objectRect.Y.ToString());
using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 2))
{
g.DrawRectangle(pen, objectRect);
}
int objectX = objectRect.X + (objectRect.Width / 2);
int objectY = objectRect.Y + (objectRect.Height / 2);
g.DrawString(objectX.ToString() + "X" + objectY.ToString(), new Font("Arial", 12), Brushes.Red, new System.Drawing.Point(250, 1));
g.Dispose();
}
}
}
So i want to recognize blue and red shapes on webcam and draw a rectangle around recognized shape. For now, I can do it as red or blue. But I want to recognize red and blue colors at the same time
how can i add multiple filters?

Showing a part of a brush on a Stackpanel

I want to show a brush on my stackpanel in the following behavior:
Brush is an image 240x120
panel is 240x60
I want to show a part of the brush like rect(0, 30, 240, 60) (so that the image on the panel is moved down a bit)
tried it with viewport and Viewbox with no result (empty Panel)
This is my code:
for (int i = 0; i < listExplorationData.Count; i++)
{
StackPanel panelLoop = new StackPanel();
panelLoop.Name = "panel_" + i.ToString();
panelLoop.Width = 240;
panelLoop.Height = 60;
panelLoop.Margin = new Thickness(0, 60 * i, 0, 0);
BitmapImage image = new BitmapImage(
new Uri("pack://application:,,,/GW2-MyWorldExploration;component/Images/" +
listExplorationData[i].mapname_en.Replace(" ", "_") +
"_loading_screen.jpg"));
ImageBrush brush = new ImageBrush();
brush.ImageSource = image;
brush.Stretch = Stretch.None;
brush.Viewport = new Rect(0, 30, 240, 60);
panelLoop.Background = brush;
mainStackPanel.Children.Add(panelLoop);
}
In order to show part of the ImageBrush you would have to set the Viewbox. If you want to specify the viewbox in absolute units, you would also have to set the ViewboxUnits property:
brush.ViewboxUnits = BrushMappingMode.Absolute;
brush.Viewbox = new Rect(0, 30, 240, 60);

Download dynamically generated image from ASP.NET website

I'm dynamically generating image from text, and existing image on my asp.net website.
Here is the code:
protected void Button1_Click(object sender, EventArgs e)
{
var tytul = Request.QueryString["Tytul"];
var tresc = Request.QueryString["Tresc"];
var font = new Font("Verdana", 23);
var brushForeColor = new SolidBrush(Color.Black);
var brushBackColor = new SolidBrush(Color.FromArgb(248, 247, 182));
var test = new Bitmap(450, 60);
var graphicstest = Graphics.FromImage(test);
var width = (int)graphicstest.MeasureString(tresc, font).Width;
var height = (int)graphicstest.MeasureString(tresc, font).Height;
while (width > 450)
{
height = height + 25;
width = width - 450;
}
var heightall = height + 40 + 30 + 100;
var bitmap = new Bitmap(450, heightall);
var graphics = Graphics.FromImage(bitmap);
var displayRectangle = new Rectangle(new Point(0, 0), new Size(450, 40));
graphics.FillRectangle(brushBackColor, displayRectangle);
//Define string format
var format1 = new StringFormat(StringFormatFlags.NoClip);
format1.Alignment = StringAlignment.Center;
var format2 = new StringFormat(format1);
//Draw text string using the text format
graphics.DrawString(tytul, font, brushForeColor, displayRectangle, format2);
// Rysowanie drugiego boxa
brushBackColor = new SolidBrush(Color.FromArgb(253, 253, 202));
font = new Font("Verdana", 18);
displayRectangle = new Rectangle(new Point(0, 40), new Size(450, height + 30));
graphics.FillRectangle(brushBackColor, displayRectangle);
displayRectangle = new Rectangle(new Point(0, 55), new Size(450, height + 15));
graphics.DrawString(tresc, font, brushForeColor, displayRectangle, format2);
graphics.DrawImage(System.Drawing.Image.FromFile(Server.MapPath(".") + "/gfx/layout/podpis.png"), new Point(0, height + 70));
Response.ContentType = "image/png";
bitmap.Save(Response.OutputStream, ImageFormat.Png);
}
As you can see the bitmap is saved and showed on aspx page after postback. What I wanna do is when user click Button1, then image is generated and browser download window pops up, without saving on server or showing on page. How to do this? Please help me.
Cheers.
You need to add a Content-Disposition header.
After you save the file:
Response.AppendHeader("content-disposition", "attachment; filename=podpis.png" );
Response.WriteFile("yourfilepath/podpis.png");
Response.End;

Draws the Highlight over the top of the tabpage

i am using TabControl in my windows form application (c#)
and
i want draw the Highlight over the top of the tabpage headers using hightlightcolor.
http://img4up.com/up2/83871411772596923665.jpg
thanks
I hope this will help you. Just apply this below mentioned code in OnDrawItem Event of Tab Control
if (e.Index == SelectedIndex)
{
Rectangle rect = new Rectangle(e.Bounds.X + 4, e.Bounds.Y, e.Bounds.Width - 6, e.Bounds.Height);
backColorBrush = new SolidBrush(Color.CornflowerBlue);
e.Graphics.FillRectangle(backColorBrush, rect);
string tabName = this.TabPages[e.Index].Text;
TabPages[e.Index].BackColor = Color.AliceBlue;
TabPages[e.Index].BorderStyle = BorderStyle.None;
TabPages[e.Index].UseVisualStyleBackColor = false;
TabPages[e.Index].RightToLeft = RightToLeft.No;
myFormat.Alignment = StringAlignment.Near;
myFont = new Font(e.Font, FontStyle.Bold);
RectangleF r1 = new RectangleF(e.Bounds.X + 1, e.Bounds.Y + 4, e.Bounds.Width, e.Bounds.Height - 4);
foreColorBrush = new System.Drawing.SolidBrush(Color.Black);
e.Graphics.DrawString(tabName, myFont, foreColorBrush, r1, myFormat);
// e.Graphics.DrawImage(img, new Point(rect.X + (GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y));
}
else
{
myFont = new Font(e.Font, FontStyle.Bold);
backColorBrush = new System.Drawing.SolidBrush(Color.CadetBlue);
foreColorBrush = new System.Drawing.SolidBrush(Color.Black);
TabPages[e.Index].BackColor = Color.AliceBlue;
string tabName = TabPages[e.Index].Text;
Rectangle rect = new Rectangle(e.Bounds.X + 1, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height + 1);
myFormat.Alignment = StringAlignment.Near;
e.Graphics.FillRectangle(backColorBrush, rect);
RectangleF r1 = new RectangleF(e.Bounds.X, e.Bounds.Y + 4, e.Bounds.Width, e.Bounds.Height - 4);
e.Graphics.DrawString(tabName, myFont, foreColorBrush, r1, myFormat);
//e.Graphics.DrawImage(img, new Point(rect.X + (GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y));
}
myFormat.Dispose();
myFont.Dispose();
backColorBrush.Dispose();
foreColorBrush.Dispose();

Categories