2016-07-22 65 views
1

我設法從inkCanvas上的繪圖中獲取了需要裁剪的區域的x,y,height,width。但我無法找到合適的解決方案來裁剪該區域並將其保存爲圖像。作品inkCanvas從特定區域繪製,Windows通用應用程序

編輯: 這裏是整個故事: 我公司開發的繪圖應用程序在一年前爲Windows Phone的8.1的Silverlight。使用這個例子

http://bsubramanyamraju.blogspot.com/2014/03/windows-phone-ink-supportsignature.html

現在我試圖讓同樣的事情寡婦普遍一些不同的功能。我正在從頭開發它,因爲現在可以在win10中使用墨跡,我不能使用舊代碼,因爲它包含僅適用於Silverlight應用程序的InkPresenter。

這裏是我用來種植面積上次的代碼(這不是我自己的代碼,我得到了它的在線)

static WriteableBitmap CropImage(WriteableBitmap source, 
                  int xOffset, int yOffset, 
                  int width, int height) 
    { 
     // Get the width of the source image 
     var sourceWidth = source.PixelWidth; 

     // Get the resultant image as WriteableBitmap with specified size 
     var result = new WriteableBitmap(width, height); 

     // Create the array of bytes 
     for (var x = 0; x <= height - 1; x++) 
     { 
      var sourceIndex = xOffset + (yOffset + x) * sourceWidth; 
      var destinationIndex = x * width; 

      Array.Copy(source.Pixels, sourceIndex, result.Pixels, destinationIndex, width); 
     } 
     return result; 
    } 

但現在說

'WriteableBitmap' does not contain a definition for 'Pixels' 

source.Pixelssource.Pixels,我不知道如何解決它。

我發佈了這個問題,希望能夠直接裁剪InkCanvas區域,因爲它現在是win10的一部分。

編輯2 @Jay的寬度和高度,我傳遞參數是我想相對於x和y要裁剪的區域的寬度和高度。我嘗試調試以檢查decoder.PixelWidthheight的值。它總是與我在參數中提供的寬度和高度相同。

所以現在如果 xoffset =185yoffset=100heightwidth在params爲5060decoder.PixelWidthdecoder.PixelHeight也將與params相同。這就是,如果條件是什麼樣子

if (pixelWidth > decoder.PixelWidth - xOffset || pixelHeight > decoder.PixelHeight - yOffset)

if (60> 60 - 185 || 50> 50- 100)

if (60> -125 || 50> -50)

因此這種情況是總是如此。我哪裏錯了?

希望我在這個編輯中沒有任何錯別字。

+0

到目前爲止您嘗試了什麼? – AVK

+0

@AVKNaidu請檢查編輯 – saira

回答

0

如您所知,UWP中的InkCanvas與Windows Phone 8.1 Silverlight中的InkCanvas不同。有關如何在UWP中使用InkCanvas,請參閱Pen and stylus interactions in UWP apps

要從特定區域裁剪inkCanvas繪圖,我們可以先使用IInkStrokeContainer.SaveAsync method存儲墨跡筆劃,然後使用BitmapTransform class裁剪圖像。例如:

public static async System.Threading.Tasks.Task<WriteableBitmap> CropImageAsync(InkCanvas source, int xOffset, int yOffset, int pixelWidth, int pixelHeight) 
{ 
    if (source.InkPresenter.StrokeContainer.GetStrokes().Count > 0) 
    { 
     using (var memStream = new InMemoryRandomAccessStream()) 
     { 
      await source.InkPresenter.StrokeContainer.SaveAsync(memStream); 

      BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memStream); 

      //pixelWidth and pixelHeight must less than the available pixel width and height 
      if (pixelWidth > decoder.PixelWidth - xOffset || pixelHeight > decoder.PixelHeight - yOffset) 
      { 
       return null; 
      } 

      BitmapTransform transform = new BitmapTransform(); 
      BitmapBounds bounds = new BitmapBounds(); 
      bounds.X = (uint)xOffset; 
      bounds.Y = (uint)yOffset; 
      bounds.Width = (uint)pixelWidth; 
      bounds.Height = (uint)pixelHeight; 
      transform.Bounds = bounds; 

      // Get the cropped pixels within the bounds of transform. 
      PixelDataProvider pix = await decoder.GetPixelDataAsync(
       BitmapPixelFormat.Bgra8, // WriteableBitmap uses BGRA format 
       BitmapAlphaMode.Straight, 
       transform, 
       ExifOrientationMode.IgnoreExifOrientation, 
       ColorManagementMode.DoNotColorManage); 

      byte[] pixels = pix.DetachPixelData(); 

      var cropBmp = new WriteableBitmap(pixelWidth, pixelHeight); 

      // Stream the bytes into a WriteableBitmap 
      using (Stream stream = cropBmp.PixelBuffer.AsStream()) 
      { 
       await stream.WriteAsync(pixels, 0, pixels.Length); 
      } 

      return cropBmp; 
     } 
    } 
    else 
    { 
     return null; 
    } 
} 
+0

它總是進入這個條件並返回null if(pixelWidth> decoder.PixelWidth - xOffset || pixelHeight> decoder.PixelHeight - yOffset) { return null; } – saira

+0

@saira保存的筆畫大小與InkCanvas的大小不同,您可能需要確保裁剪區域小於繪圖區域。您可以使用'decoder.PixelWidth'和'decoder.PixelHeight'來查看保存的筆畫大小。 –

+0

請檢查edit2 – saira

相關問題