Skip to content

Commit

Permalink
Add save-as-png function for Windows/CairoSamples.
Browse files Browse the repository at this point in the history
  • Loading branch information
zwcloud committed Oct 17, 2016
1 parent eeef2cb commit 14d2962
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
40 changes: 25 additions & 15 deletions Demo/Windows/CairoSamples/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Demo/Windows/CairoSamples/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public partial class Form1 : Form
public Win32Surface Surface1 { get; private set; }

private readonly byte[] romedalenPngData;
private string lastSelected = "none";

public Form1()
{
Expand Down Expand Up @@ -40,6 +41,7 @@ protected override void OnPaint(PaintEventArgs e)

private void arcToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "arc";
OnPaintAction = cr =>
{
double xc = 128.0;
Expand Down Expand Up @@ -71,6 +73,7 @@ private void arcToolStripMenuItem_Click(object sender, EventArgs e)

private void arcNegativeToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "arcNegative";
OnPaintAction = cr =>
{
double xc = 128.0;
Expand Down Expand Up @@ -102,6 +105,7 @@ private void arcNegativeToolStripMenuItem_Click(object sender, EventArgs e)

private void clipToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "clip";
OnPaintAction = cr =>
{
cr.Arc(128.0, 128.0, 76.8, 0, 2 * Math.PI);
Expand All @@ -124,6 +128,7 @@ private void clipToolStripMenuItem_Click(object sender, EventArgs e)

private void clipImageToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "clipImage";
OnPaintAction = cr =>
{
int w, h;
Expand All @@ -150,6 +155,7 @@ private void clipImageToolStripMenuItem_Click(object sender, EventArgs e)

private void curveRectangleToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "curveRectangle";
OnPaintAction = cr =>
{
/* a custom shape that could be wrapped in a function */
Expand Down Expand Up @@ -222,6 +228,7 @@ private void curveRectangleToolStripMenuItem_Click(object sender, EventArgs e)

private void curveToToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "curveTo";
OnPaintAction = cr =>
{
double x = 25.6, y = 128.0;
Expand All @@ -247,6 +254,7 @@ private void curveToToolStripMenuItem_Click(object sender, EventArgs e)

private void dashToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "dash";
OnPaintAction = cr =>
{
double[] dashes =
Expand Down Expand Up @@ -274,6 +282,7 @@ private void dashToolStripMenuItem_Click(object sender, EventArgs e)

private void fillAndStroke2ToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "fillAndStroke2";
OnPaintAction = cr =>
{
cr.MoveTo(128.0, 25.6);
Expand All @@ -300,6 +309,7 @@ private void fillAndStroke2ToolStripMenuItem_Click(object sender, EventArgs e)

private void fillStyleToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "fillStyle";
OnPaintAction = cr =>
{
cr.LineWidth = 6;
Expand Down Expand Up @@ -327,6 +337,7 @@ private void fillStyleToolStripMenuItem_Click(object sender, EventArgs e)

private void gradientToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "gradient";
OnPaintAction = cr =>
{
Gradient pat = new LinearGradient(0.0, 0.0, 0.0, 256.0);
Expand All @@ -352,6 +363,7 @@ private void gradientToolStripMenuItem_Click(object sender, EventArgs e)

private void imageToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "image";
OnPaintAction = cr =>
{
int w, h;
Expand All @@ -376,6 +388,7 @@ private void imageToolStripMenuItem_Click(object sender, EventArgs e)

private void imagepatternToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "imagepattern";
OnPaintAction = cr =>
{
int w, h;
Expand Down Expand Up @@ -413,6 +426,7 @@ private void imagepatternToolStripMenuItem_Click(object sender, EventArgs e)

private void multiSegmentCapsToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "multiSegmentCaps";
OnPaintAction = cr =>
{
cr.MoveTo(50.0, 75.0);
Expand All @@ -434,6 +448,7 @@ private void multiSegmentCapsToolStripMenuItem_Click(object sender, EventArgs e)

private void roundedRectangleToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "roundedRectangle";
OnPaintAction = cr =>
{
/* a custom shape that could be wrapped in a function */
Expand Down Expand Up @@ -466,6 +481,7 @@ private void roundedRectangleToolStripMenuItem_Click(object sender, EventArgs e)

private void setLineCapToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "setLineCap";
OnPaintAction = cr =>
{
cr.LineWidth = 30.0;
Expand Down Expand Up @@ -493,6 +509,7 @@ private void setLineCapToolStripMenuItem_Click(object sender, EventArgs e)

private void setLineJoinToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "setLineJoin";
OnPaintAction = cr =>
{
cr.LineWidth = 40.96;
Expand Down Expand Up @@ -520,6 +537,7 @@ private void setLineJoinToolStripMenuItem_Click(object sender, EventArgs e)

private void textToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "text";
OnPaintAction = cr =>
{
cr.SelectFontFace("Microsoft Sans Serif", FontSlant.Normal, FontWeight.Bold);
Expand Down Expand Up @@ -549,6 +567,7 @@ private void textToolStripMenuItem_Click(object sender, EventArgs e)

private void textAlignCenterToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "textAlignCenter";
OnPaintAction = cr =>
{
string text = "cairo";
Expand Down Expand Up @@ -582,6 +601,7 @@ private void textAlignCenterToolStripMenuItem_Click(object sender, EventArgs e)

private void textExtentsToolStripMenuItem_Click(object sender, EventArgs e)
{
lastSelected = "textExtents";
OnPaintAction = cr =>
{
string text = "cairo";
Expand Down Expand Up @@ -613,5 +633,21 @@ private void textExtentsToolStripMenuItem_Click(object sender, EventArgs e)

Invalidate();
}

private void saveAsPngToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "PNG|*.png";
dialog.FileName = lastSelected;
if (dialog.ShowDialog() == DialogResult.OK)
{
using (this.Graphics1 = this.CreateGraphics())
using (Surface1 = new Win32Surface(Graphics1.GetHdc()))
using (Context1 = new Context(Surface1))
{
Surface1.WriteToPng(dialog.FileName);
}
}
}
}
}

0 comments on commit 14d2962

Please sign in to comment.