0

以下のコード行が{"InvalidCastException"}を引き起こすのはなぜですか

((RotateTransform)image.RenderTransform).Angle = 90; 

メソッドのコード全体は次のとおりです。

void loadImage()
        {
            // The image will be read from isolated storage into the following byte array

        byte[] data;

        // Read the entire image in one go into a byte array

        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {

            // Open the file - error handling omitted for brevity

            // Note: If the image does not exist in isolated storage the following exception will be generated:

            // System.IO.IsolatedStorage.IsolatedStorageException was unhandled 

            // Message=Operation not permitted on IsolatedStorageFileStream 

            using (IsolatedStorageFileStream isfs = isf.OpenFile("0.jpg", FileMode.Open, FileAccess.Read))
            {

                // Allocate an array large enough for the entire file

                data = new byte[isfs.Length];



                // Read the entire file and then close it

                isfs.Read(data, 0, data.Length);

                isfs.Close();

            }
        }



        // Create memory stream and bitmap

        MemoryStream ms = new MemoryStream(data);

        BitmapImage bi = new BitmapImage();

        // Set bitmap source to memory stream

        bi.SetSource(ms);

        // Create an image UI element – Note: this could be declared in the XAML instead

        Image image = new Image();

        // Set size of image to bitmap size for this demonstration

        image.Height = bi.PixelHeight;

        image.Width = bi.PixelWidth;

        // Assign the bitmap image to the image’s source

        image.Source = bi;

        ((RotateTransform)image.RenderTransform).Angle += 90; 

        // Add the image to the grid in order to display the bit map

        ContentPanelx.Children.Add(image);

    }

編集

次のコードをこのように変更すると、クラッシュしませんが、画像は描画されません。

        image.Height = bi.PixelHeight;

        image.Width = bi.PixelWidth;

        // Assign the bitmap image to the image’s source

        image.Source = bi;
        image.RenderTransform = new RotateTransform() { Angle = 90 };

        ContentPanelx.Children.Add(image);

私が見逃しているステップはありますか?

どうもありがとう、-コード

4

3 に答える 3

2

これで、画像オブジェクトが作成されました。そのプロパティはインスタンスRenderTransformを参照しません。RotateTransform試す:image.RenderTransform = new RotateTransform(){Angle=90};

于 2012-04-26T12:02:44.807 に答える
0

単純な回答レベルでは、RenderTransformにRotateTransformを含めるように実際に割り当てていることはないように見えます。したがって、キャストが失敗するのは当然のことです。

より完全な答えとして:

image.RenderTransform = new RotateTransform() {Angle = 45.0};

于 2012-04-26T12:08:15.090 に答える
0

RenderTransformプロパティのデフォルト値はですTransform.Identity。あなたがそれを操作することができる前にあなたはRotateTransformあなたにを適用しなければなりません。Image

image.RenderTransform = new RotateTransform();
于 2012-04-26T12:26:44.433 に答える