sample: Add sample that uses only cairo and writes image files

The sample writes the same PNG file over and over, with some pauses in
between iterations. Some indicative numbers are written to the console,
to help track performance and memory usage.

Feel free to add more elaborate drawing to this sample, to exercise more
of the cairo API.
This commit is contained in:
Bertrand Lorentz 2013-11-24 15:22:05 +01:00
parent 51f102bc34
commit 21b081b6a9
3 changed files with 82 additions and 1 deletions

76
sample/CairoPng.cs Normal file
View File

@ -0,0 +1,76 @@
using System;
using System.Diagnostics;
using System.IO;
using Cairo;
public class CairoPng
{
public void CreatePng ()
{
const int Width = 480;
const int Height = 160;
const int CheckSize = 10;
const int Spacing = 2;
// Create an Image-based surface with data stored in ARGB32 format and a context,
// "using" is used here to ensure that they are Disposed once we are done
using (ImageSurface surface = new ImageSurface (Format.ARGB32, Width, Height))
using (Context cr = new Cairo.Context (surface))
{
// Start drawing a checkerboard
int i, j, xcount, ycount;
xcount = 0;
i = Spacing;
while (i < Width) {
j = Spacing;
ycount = xcount % 2; // start with even/odd depending on row
while (j < Height) {
if (ycount % 2 != 0)
cr.SetSourceRGB (1, 0, 0);
else
cr.SetSourceRGB (1, 1, 1);
// If we're outside the clip, this will do nothing.
cr.Rectangle (i, j, CheckSize, CheckSize);
cr.Fill ();
j += CheckSize + Spacing;
++ycount;
}
i += CheckSize + Spacing;
++xcount;
}
// Select a font to draw with
cr.SelectFontFace ("serif", FontSlant.Normal, FontWeight.Bold);
cr.SetFontSize (64.0);
// Select a color (blue)
cr.SetSourceRGB (0, 0, 1);
// Draw
cr.MoveTo (20, 100);
cr.ShowText ("Hello, World");
surface.WriteToPng ("test.png");
}
}
static void Main ()
{
var app = new CairoPng ();
int iterations = 100;
for (int loop = 0; loop < 10; loop++) {
Stopwatch stop_watch = new Stopwatch ();
stop_watch.Start ();
Console.Write ("Starting iterations, {0} bytes used...\t", Process.GetCurrentProcess().PrivateMemorySize64);
for (int i = 0; i < iterations; i++) {
app.CreatePng ();
}
stop_watch.Stop ();
Console.WriteLine ("Created {0} PNG files in {1}ms", iterations, stop_watch.ElapsedMilliseconds);
System.Threading.Thread.Sleep (1000);
}
}
}

View File

@ -8,7 +8,7 @@ DOTNET_TARGETS=
DOTNET_ASSEMBLY=
endif
TARGETS = gtk-hello-world.exe async-sample.exe button.exe calendar.exe subclass.exe menu.exe treeviewdemo.exe managedtreeviewdemo.exe nodeviewdemo.exe treemodeldemo.exe actions.exe spawn.exe assistant.exe registerprop.exe gexceptiontest.exe native-instantiation.exe polarfixed.exe cairo-sample.exe scribble.exe testdnd.exe custom-cellrenderer.exe custom-widget.exe custom-scrollable.exe #scribble-xinput.exe $(DOTNET_TARGETS)
TARGETS = gtk-hello-world.exe async-sample.exe button.exe calendar.exe subclass.exe menu.exe treeviewdemo.exe managedtreeviewdemo.exe nodeviewdemo.exe treemodeldemo.exe actions.exe spawn.exe assistant.exe registerprop.exe gexceptiontest.exe native-instantiation.exe polarfixed.exe cairo-sample.exe scribble.exe testdnd.exe custom-cellrenderer.exe custom-widget.exe custom-scrollable.exe cairo-png.exe #scribble-xinput.exe $(DOTNET_TARGETS)
DEBUGS = $(addsuffix .mdb, $(TARGETS))
@ -103,6 +103,9 @@ registerprop.exe: $(srcdir)/PropertyRegistration.cs $(assemblies)
gexceptiontest.exe: $(srcdir)/GExceptionTest.cs $(assemblies)
$(CSC) $(CSFLAGS) -out:gexceptiontest.exe $(references) $(srcdir)/GExceptionTest.cs
cairo-png.exe: $(srcdir)/CairoPng.cs $(top_builddir)/cairo/cairo-sharp.dll
$(CSC) $(CSFLAGS) -out:cairo-png.exe -r:$(top_builddir)/cairo/cairo-sharp.dll $(srcdir)/CairoPng.cs
EXTRA_DIST = \
HelloWorld.cs \
Assistant.cs \
@ -120,6 +123,7 @@ EXTRA_DIST = \
NativeInstantiationTest.cs \
NodeViewDemo.cs \
GExceptionTest.cs \
CairoPng \
CairoSample.cs \
TestDnd.cs \
CustomCellRenderer.cs \

View File

@ -108,6 +108,7 @@
<Compile Include="gio\AppInfo.cs" />
<Compile Include="gio\Volume.cs" />
<Compile Include="gtk-gio\MountOperation.cs" />
<Compile Include="CairoPng.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>