2009-08-17 91 views
1

我需要將傳遞給函數的圖像作爲BitmapFrame的數組旋轉。成品需要保存爲BitmapFrame,所以我可以將它發送到我的Export-Image函數。幫幫我?我需要旋轉圖像

[Cmdlet(VerbsData.ConvertTo, "Rotate")] 
public class RotateCmdlet : PSCmdlet 
{ 
    private BitmapFrame[] bFrame, outFrame; 
    private BitmapSource src; 
    private double pixelsize; 
    private int degrees; 
    private byte[] pixels, outPixels; 

    [Parameter(ValueFromPipeline = true, 
     ValueFromPipelineByPropertyName = true), ValidateNotNullOrEmpty] 
    public BitmapFrame[] Bitmap 
    { 
     get 
     { 
      return bFrame; 
     } 
     set 
     { 
      bFrame = value; 
     } 
    } 

    [Parameter(Position = 0), ValidateNotNullOrEmpty] 
    public int Degrees 
    { 
     get 
     { 
      return degrees; 
     } 
     set 
     { 
      degrees = value; 
     } 
    } 

    protected override void ProcessRecord() 
    { 
     base.ProcessRecord(); 
     Console.Write("Rotating the image {0} degrees...\n\n", degrees); 
     outFrame = new BitmapFrame[bFrame.Length]; 
     for (int c = 0; c < bFrame.Length; c++) 
     { 
      Image image; 

      pixelsize = bFrame[c].PixelWidth * bFrame[c].PixelHeight; 
      pixels = new byte[(int)pixelsize]; 
      outPixels = new byte[(int)pixelsize]; 
      bFrame[c].CopyPixels(pixels, (int)(bFrame[c].Width * (bFrame[c].Format.BitsPerPixel/8.0)), 0); 

      Stream strm = new MemoryStream(pixels); 
      image = Image.FromStream(strm); 

      var newBitmap = new Bitmap((int)bFrame[c].PixelWidth, (int)bFrame[c].PixelHeight); 
      var graphics = Graphics.FromImage(newBitmap); 
      graphics.TranslateTransform((float)bFrame[c].PixelWidth/2, (float)bFrame[c].PixelHeight/2); 
      graphics.RotateTransform(degrees); 
      graphics.TranslateTransform(-(float)bFrame[c].PixelWidth/2, -(float)bFrame[c].PixelHeight/2); 
      graphics.DrawImage(image, new System.Drawing.Point(0, 0)); 

      for (int i = 0; i < pixelsize; i++) 
      { 
       outPixels[i] = pixels[i]; 
      } 

      src = BitmapSource.Create(bFrame[c].PixelWidth, bFrame[c].PixelHeight, bFrame[c].DpiX, bFrame[c].DpiY, 
       bFrame[c].Format, bFrame[c].Palette, outPixels, (int)(bFrame[c].Width * (bFrame[c].Format.BitsPerPixel/8))); 
      outFrame[c] = BitmapFrame.Create(src); 
     } 
     WriteObject(outFrame); 
    } 
} 
+0

有點多信息會很好:)例如,你發佈的代碼有什麼問題? – cwap 2009-08-17 20:14:21

+0

此外(與您的問題無關),您想對您在此處使用的所有內容(如Stream,Bitmap和Graphics)調用Dispose()(或在using()塊中使用它們)。 – MusiGenesis 2009-08-17 20:41:22

+0

您或許可以在此處推導出有關dabonz413問題的更多信息:http://www.stackoverflow.com/questions/1190743/我還沒有時間仔細閱讀代碼,幾周後我就問了這個問題以前,所以我不完全清新。但如果我最終找到更多有用的(或上下文)信息,我會在這裏再次發帖。 – Giffyguy 2009-08-17 20:58:39

回答

1

看來您在FromStream調用中發生的錯誤可能是因爲圖像格式無效。這可能有很多原因,因爲我不確定這個Cmdlet是如何使用的,所以我會做一個假設:

我可能在這裏不正確,但是因爲你傳遞了一個BitMapFrame的數組, m想知道是否爲每個數組元素調用一次ProcessRecord。真正講述的唯一方法是看你是如何調用Cmdlet的。例如,如果您的BitMap參數來自管道,那麼對於陣列中的每個BitMapFrame都有一次調用ProcessRecord的好機會。

這有道理嗎?

+0

這是我放在命令提示符下的命令:導入圖像E:\ RawConvert \ lenna.arf | ConvertTo-Rotate 90 |出口圖像E:\ RawConvert \ rotate.jpg 它得到所有的幀到在導入一個陣列,發送到旋轉小命令(90度)與框架通管道被髮送。但我認爲,因爲唯一的循環是在ProcessRecord中,它只會調用一次? 我的圖像格式有什麼問題? – dabonz413 2009-08-19 15:35:28

+0

我只是好奇Cmdlet是如何被調用的,因爲如果它是管道的一部分並且foreach對象也被使用,那麼ProcessRecord每次都會被調用。如果是這種情況,那麼圖像格式將會是錯誤的,並且由於爲什麼FromStream失敗(即它只包含BitMapFrame的一部分),它將會產生影響。 – 2009-08-19 16:00:53

+0

只要確認一下,你是否可以驗證當你像上面指出的那樣運行命令時,你的ConvertTo-Rotate90 Cmdlet只被調用一次?對於Import-Image傳遞給它的數組中的每個元素,它似乎都被調用了一次。 – 2009-08-19 16:31:55