2016-02-29 2 views
0

Je m'attends à obtenir un rectangle de couleur, mais en obtenant le rectangle de la poubelle à la place. Jusqu'à présent, j'ai le code suivant:Comment dessiner avec Skia sur le contexte du Caire dans Gtk3

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(); 
     } 
    } 
} 

Répondre

0

J'ai trouvé le Soulution je l'ai utilisé SKBitmap avant de créer SKSurface et SKCanvas. Pour obtenir des données de pixels, j'aurais dû utiliser la méthode SKBitmap.GetPixels. Voici suit le code source de l'exemple de travail:

using System; 
    using GLib; 
    using SkiaSharp; 
    using Gtk; 

    namespace SkiaSharpExample 
    { 
     class CCDrawingArea : DrawingArea 
     { 
      protected override bool OnDrawn(Cairo.Context cr) 
      { 
       const int width = 100; 
       const int height = 100; 

       using (var bitmap = new SKBitmap(width, height, SKColorType.N_32, SKAlphaType.Premul)) 
       { 
        IntPtr len; 
        using (var skSurface = SKSurface.Create(bitmap.Info.Width, bitmap.Info.Height, SKColorType.N_32, SKAlphaType.Premul, bitmap.GetPixels(out len), bitmap.Info.RowBytes)) 
        { 
         var canvas = skSurface.Canvas; 
         canvas.Clear(SKColors.White); 

         using (var paint = new SKPaint()) 
         { 
          paint.StrokeWidth = 4; 
          paint.Color = new SKColor(0x2c, 0x3e, 0x50); 

          var rect = new SKRect(10, 10, 50, 50); 
          canvas.DrawRect(rect, paint); 
         } 

         Cairo.Surface surface = new Cairo.ImageSurface(
          bitmap.GetPixels(out len), 
          Cairo.Format.Argb32, 
          bitmap.Width, bitmap.Height, 
          bitmap.Width * 4); 


         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 Skia 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(); 
      } 

      void OnException(object o, UnhandledExceptionArgs args) 
      { 

      } 
     } 
    } 
+0

Ce code ne fonctionnera que sur Windows où '' Cairo.Format.Argb32' et SKColorType .N_32' définit la même commande ARGB. Sur Linux et Mac 'SKColorType.N_32' est' A-B-G-R' à la place. –

0

Je n'ai pas la moindre idée de Skia et je ne peux trouver aucune documentation sur son format d'image, mais le dernier argument ici devrait être la foulée. La foulée naturelle serait 4*image.Width. Est-ce ce que Skia utilise aussi? (La foulée est le nombre d'octets entre le début d'un pixel et le pixel inférieur à celui d'un)

  Cairo.Surface surface = new Cairo.ImageSurface (
       image.Encode().Data, 
       Cairo.Format.Argb32, 
       image.Width, image.Height, 
       4 * image.Width * image.Height); 
+0

Oui, qui faisait partie du problème, je devrais utiliser 4 * Image.Width –