2010-06-28 39 views

回答

8

我寫過這樣一個插件一次。 暫時忘掉模板,你可以從頭開始。

首先添加對PaintDotnet.Base,PaintDotNet.Core和PaintDotNet.Data的引用。

接下來,您將需要一個類從文件類型類繼承:

例如:

public class SampleFileType : FileType 
{ 
    public SampleFileType() : base ("Sample file type", FileTypeFlags.SupportsSaving | 
           FileTypeFlags.SupportsLoading, 
           new string[] { ".sample" }) 
    { 

    } 

    protected override void OnSave(Document input, System.IO.Stream output, SaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback) 
    { 
     //Here you get the image from Paint.NET and you'll have to convert it 
     //and write the resulting bytes to the output stream (this will save it to the specified file) 

     using (RenderArgs ra = new RenderArgs(new Surface(input.Size))) 
     { 
      //You must call this to prepare the bitmap 
        input.Render(ra); 

      //Now you can access the bitmap and perform some logic on it 
      //In this case I'm converting the bitmap to something else (byte[]) 
      var sampleData = ConvertBitmapToSampleFormat(ra.Bitmap); 

      output.Write(sampleData, 0, sampleData.Length);     
     } 
    } 

    protected override Document OnLoad(System.IO.Stream input) 
    { 
     //Input is the binary data from the file 
     //What you need to do here is convert that date to a valid instance of System.Drawing.Bitmap 
     //In the end you need to return it by Document.FromImage(bitmap) 

     //The ConvertFromFileToBitmap, just like the ConvertBitmapSampleFormat, 
     //is simply a function which takes the binary data and converts it to a valid instance of System.Drawing.Bitmap 
     //You will have to define a function like this which does whatever you want to the data 

     using(var bitmap = ConvertFromFileToBitmap(input)) 
     { 
      return Document.FromImage(bitmap); 
     } 
    } 
} 

所以,你從文件類型繼承。在構造函數中指定支持哪些操作(加載/保存)以及應註冊哪些文件擴展名。然後,您爲保存和加載操作提供邏輯。

基本上這就是你需要的一切。

最後,你將不得不告訴Pain.Net你想加載哪些FileType類,在這種情況下是單個實例,但是你可以在單個庫中擁有多於一個的FileType類。

public class SampleFileTypeFactory : IFileTypeFactory 
{ 
    public FileType[] GetFileTypeInstances() 
    { 
     return new FileType[] { new SampleFileType() }; 
    } 

我希望這可以幫助,讓我知道如果你有問題。 }

+0

非常感謝您的幫助 我還有一些問題。什麼是令牌,scratchSurface和回調需要? – kalan 2010-06-30 10:35:03