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);
Related
I please need assistance with tabcontrol itemsize. (winforms)
I have a owner-drawn tabcontrol and all working fine. Itemsize is set to width of 0 and sizemode to fixed in the tabcontrol properties. When I open a filename eg test.txt then "button" looks something like this
|test123.txt |
|Untitled |
which is fine, however, if I open longer filenames then the "button" looks like this
|thisisaverylongfilename.txt |
|anotherlongname.txt |
I'm trying to make the "button" so that the filename just fits with some space on the right and the space should be the same for all.
Below is my code for the user drawn tabcontrol
Image cross = imageList1.Images[0];
int xWidth = cross.Width;
TabPage tp = tabControl1.TabPages[e.Index];
Size size = tabControl1.ItemSize;
Font fntTab;
Brush bshBack;
Brush bshFore;
if (e.Index == this.tabControl1.SelectedIndex)
{
fntTab = new Font(FontFamily.GenericSansSerif, 8.0F, FontStyle.Regular);
bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.FromName(tabpages_primary_backcolor), Color.FromName(tabpages_secondary_backcolor), System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
bshFore = Brushes.Firebrick;
}
else
{
fntTab = new Font(FontFamily.GenericSansSerif, 8.0F, FontStyle.Regular);
bshBack = new SolidBrush(Color.Empty);
bshFore = new SolidBrush(Color.Black);
}
string tabName = this.tabControl1.TabPages[e.Index].Text;
StringFormat sftTab = new StringFormat();
e.Graphics.FillRectangle(bshBack, e.Bounds);
Rectangle recTab = e.Bounds;
recTab = new Rectangle(recTab.X + 5, recTab.Y + 4, recTab.Width, recTab.Height - 4);
e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
if (tabControl1.SelectedTab != null)
{
currentCrossRect = new Rectangle(
e.Bounds.Left + size.Width - 22, 3, xWidth, xWidth);
e.Graphics.DrawImage(cross, currentCrossRect.X - 2, currentCrossRect.Y + 2);
}
Thanks and regards
Get the width of the text using:
SizeF s = e.Graphics.MeasureString(myString, myFont);
And then you can set the item's width to s.Width!
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;
}
I want to draw a signal image as shown below at runtime on canvas.
sample code which I used to draw this signal is as below.
Ellipse Ellipse1 = new Ellipse();
Ellipse Ellipse2 = new Ellipse();
Ellipse Ellipse3 = new Ellipse();
Line lineV = new Line();
Line lineH = new Line();
lineV.Stroke = SystemColors.WindowFrameBrush;
lineV.X1 = EndPosition.X;
lineV.Y1 = EndPosition.Y;
lineV.X2 = StartPosition.X;
lineV.Y2 = EndPosition.Y;
SolidColorBrush redBrush = new SolidColorBrush();
redBrush.Color = Colors.Black;
lineV.StrokeThickness = 2;
lineV.Stroke = redBrush;
canvas1.Children.Add(lineV);
lineH.Stroke = SystemColors.WindowFrameBrush;
lineH.X1 = StartPosition.X;
lineH.Y1 = EndPosition.Y;
lineH.X2 = StartPosition.X;
lineH.Y2 = StartPosition.Y;
redBrush.Color = Colors.Black;
lineH.StrokeThickness = 2;
lineH.Stroke = redBrush;
canvas1.Children.Add(lineH);
SolidColorBrush mySolidColorBrush1 = new SolidColorBrush();
mySolidColorBrush1.Color = Colors.Red; //FromArgb(255, 255, 255, 0);
Ellipse1.Fill = mySolidColorBrush1;
Ellipse1.StrokeThickness = 2;
Ellipse1.Stroke = Brushes.Black;
Ellipse1.Width = 30;
Ellipse1.Height = 30;
Ellipse1.Margin = new Thickness(EndPosition.X, EndPosition.Y - 15, EndPosition.X + 50, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse1);
SolidColorBrush mySolidColorBrush2 = new SolidColorBrush();
mySolidColorBrush2.Color = Colors.Green; //FromArgb(255, 255, 255, 0);
Ellipse2.Fill = mySolidColorBrush2;
Ellipse2.StrokeThickness = 2;
Ellipse2.Stroke = Brushes.Black;
Ellipse2.Width = 30;
Ellipse2.Height = 30;
Ellipse2.Margin = new Thickness(EndPosition.X + 30, EndPosition.Y - 15, EndPosition.X + 60, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse2);
SolidColorBrush mySolidColorBrush3 = new SolidColorBrush();
mySolidColorBrush3.Color = Colors.Yellow; // FromArgb(255, 255, 255, 0);
Ellipse3.Fill = mySolidColorBrush3;
Ellipse3.StrokeThickness = 2;
Ellipse3.Stroke = Brushes.Black;
Ellipse3.Width = 30;
Ellipse3.Height = 30;
Ellipse3.Margin = new Thickness(EndPosition.X + 60, EndPosition.Y - 15, EndPosition.X + 150, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse3);
**Now I want the user to be able to interactively move this signal on the canvas on mouse move events.
How can I able to do this ?**
Iam using C# WPF.
If want to implement a canvas to drag elements, a DragCanvas is your choice.
Dragging Elements in a Canvas from Josh Smith and after Dragging Elements in a Canvas from igkutikov that adapt Josh Smith code. There are articles with #mustread category.
With the dragCanvas you can implement a full functionality Dragging Element Canvas, and better adapt to your code. Happy coding!
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;
C# wpf 3D visual studio 2010 net 4.5
Hi
I am trying to print out the 3D image I have created but can not get it right.
The image that is printed is changing in size depending on how large the window is etc.
or it is clipped etc.
What I would like is to print the view port on the printer,
stretching it so wide as the paper is and keeping aspect ration.
PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() != true)
{ return; }
StackPanel myPanel = new StackPanel();
myPanel.Margin = new Thickness(40);
Image myImage = new Image();
myImage.Width = dialog.PrintableAreaWidth - (2 * MYDPI);
myImage.Stretch = Stretch.Uniform;
RenderTargetBitmap bmp = new RenderTargetBitmap((int)dialog.PrintableAreaWidth, (int)dialog.PrintableAreaWidth, 96, 96, PixelFormats.Pbgra32);
bmp.Render(myViewPort);
myImage.Source = bmp;
myPanel.Children.Add(myImage);
myPanel.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
myPanel.Arrange(new Rect(new Point(0, 0), myPanel.DesiredSize));
dialog.PrintVisual(myPanel, myName);
The following worked, now the picture get scaled to the size of the paper regardless of
the size of the viewport
...
PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() != true)
{
return;
}
Grid grid = new Grid();
grid.Margin = new Thickness(40);
//do this for each column
ColumnDefinition coldef;
coldef = new ColumnDefinition();
coldef.Width = new GridLength(dialog.PrintableAreaWidth, GridUnitType.Pixel);
grid.ColumnDefinitions.Add(coldef);
//do this for each row
RowDefinition rowdef;
rowdef = new RowDefinition();
rowdef.Height = new GridLength(1, GridUnitType.Auto);
grid.RowDefinitions.Add(rowdef);
//
rowdef = new RowDefinition();
rowdef.Height = new GridLength(1, GridUnitType.Auto);
grid.RowDefinitions.Add(rowdef);
TextBlock myTitle = new TextBlock();
myTitle.FontSize = 24;
myTitle.FontFamily = new FontFamily("Arial");
myTitle.TextAlignment = TextAlignment.Center;
myTitle.Text = myName;
grid.Children.Add(myTitle);
//put it in column 0, row 0
Grid.SetColumn(myTitle, 0);
Grid.SetRow(myTitle, 0);
Image myImage = new Image();
RenderTargetBitmap bmp = new RenderTargetBitmap((int)this.Width, (int)this.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(myViewPort);
myImage.Source = bmp;
myImage.Stretch = Stretch.Uniform;
grid.Children.Add(myImage);
//put it in column 0, row 1
Grid.SetColumn(myImage, 0);
Grid.SetRow(myImage, 1);
grid.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
grid.Arrange(new Rect(new Point(0, 0), grid.DesiredSize));
dialog.PrintVisual(grid, myName);