我需要將傳遞給函數的圖像作爲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);
}
}
有點多信息會很好:)例如,你發佈的代碼有什麼問題? – cwap 2009-08-17 20:14:21
此外(與您的問題無關),您想對您在此處使用的所有內容(如Stream,Bitmap和Graphics)調用Dispose()(或在using()塊中使用它們)。 – MusiGenesis 2009-08-17 20:41:22
您或許可以在此處推導出有關dabonz413問題的更多信息:http://www.stackoverflow.com/questions/1190743/我還沒有時間仔細閱讀代碼,幾周後我就問了這個問題以前,所以我不完全清新。但如果我最終找到更多有用的(或上下文)信息,我會在這裏再次發帖。 – Giffyguy 2009-08-17 20:58:39