2009-08-17 92 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