我期望得到彩色矩形,但得到矩形垃圾。到目前为止,我有以下代码:
using System;
using GLib;
using SkiaSharp;
using Gtk;
namespace SkiaSharpExample {
class CCDrawingArea : DrawingArea {
protected override bool OnDrawn (Cairo.Context cr) {
using (var skSurface = SKSurface.Create (100, 100, SKColorType.N_32, SKAlphaType.Premul)) {
var canvas = skSurface.Canvas;
var paint = new SKPaint {
StrokeWidth = 4,
Color = new SKColor (255, 255, 255, 255)
};
var rect = new SKRect (10, 10, 50, 50);
canvas.DrawRect (rect, paint);
var image = skSurface.Snapshot ();
Cairo.Surface surface = new Cairo.ImageSurface (
image.Encode ().Data,
Cairo.Format.Argb32,
image.Width, image.Height,
4 * image.Width * image.Height);
surface.MarkDirty ();
cr.SetSourceSurface (surface, 0, 0);
cr.Paint ();
}
return true;
}
}
class MainClass {
public static void Main (string[] args){
ExceptionManager.UnhandledException += delegate(UnhandledExceptionArgs expArgs) {
Console.WriteLine (expArgs.ExceptionObject.ToString ());
expArgs.ExitApplication = true;
};
Gtk.Application.Init ();
var window = new Window ("Hello World");
window.SetDefaultSize (1024, 800);
window.SetPosition (WindowPosition.Center);
window.DeleteEvent += delegate {
Gtk.Application.Quit ();
};
var darea = new CCDrawingArea ();
window.Add (darea);
window.ShowAll ();
Gtk.Application.Run ();
}
}
}