none
BitmapTransformを使用した回転と拡大縮小について RRS feed

  • 質問

  • こんにちは。

     BitmapDecoderオブジェクトを使用して、画像の回転,拡大縮小を行い、
     回転、拡大縮小後のバイト配列を取得したいのでがうまくいきません。
     実装は以下になります。画像データ横長(100px x 50px)などの場合、
     回転と拡大縮小を実装するには、どうすればいいのでしょうか?

                IRandomAccessStreamWithContentType logoStream = await logoFile.OpenReadAsync();
                BitmapDecoder logoData = await BitmapDecoder.CreateAsync(logoStream);

                uint width;
                uint height;
                byte[] bytes = default(byte[]);


                width = (int)logoData.PixelWidth;
                height = (int)logoData.PixelHeight;
                BitmapTransform transform = new BitmapTransform();
                BitmapBounds bounds = new BitmapBounds();
                bounds.X = 0 ;
                bounds.Y = 0 ;
                bounds.Height = height;
                bounds.Width = width;
                transform.Bounds = bounds;

                transform.ScaledWidth  = width;
                transform.ScaledHeight = height;
               
                //transform.Rotation = BitmapRotation.Clockwise180Degrees;
                //transform.Rotation = BitmapRotation.Clockwise90Degrees;
                //transform.InterpolationMode = BitmapInterpolationMode.Fant;


                PixelDataProvider pixelData = await logoData.GetPixelDataAsync(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Straight,
                    transform,
                    ExifOrientationMode.IgnoreExifOrientation,
                    ColorManagementMode.ColorManageToSRgb);
               
                bytes = pixelData.DetachPixelData();

                var bitmap = new WriteableBitmap(width, height);

                using (var pixelStream = bitmap.PixelBuffer.AsStream())
                {
                    await pixelStream.WriteAsync(bytes, 0, bytes.Length);
                }

                byte[] pixels = default(byte[]);
                pixels = bitmap.PixelBuffer.ToArray();

    2015年10月6日 9:11

回答

  • こんにちは。

    どううまく行かないのですか?

    横長の話をされていたのでひとつ気になったのは、例えばClockwise90Degreesで90度の回転をさせた場合は
    ScaleWidth, ScaleHeight、WriteableBitmapのサイズとか色々サイズを調整すると思うのですが、その辺りは大丈夫ですか。

    //transform.ScaledWidth = width;      //★
    //transform.ScaledHeight = height;    //★
    transform.ScaledWidth = height;
    transform.ScaledHeight = width;
    
    //transform.Rotation = BitmapRotation.Clockwise180Degrees;
    transform.Rotation = BitmapRotation.Clockwise90Degrees;
    //transform.InterpolationMode = BitmapInterpolationMode.Fant;
    
    PixelDataProvider pixelData = await logoData.GetPixelDataAsync(
        BitmapPixelFormat.Bgra8,
        BitmapAlphaMode.Straight,
        transform,
        ExifOrientationMode.IgnoreExifOrientation,
        ColorManagementMode.ColorManageToSRgb);
    
    bytes = pixelData.DetachPixelData();
    
    //var bitmap = new WriteableBitmap((int)width, (int)height); ★
    var bitmap = new WriteableBitmap((int)height, (int)width);
    

    バイト配列自体はHongliangさんが仰っているとおり既に取得済みだと思います。

    • 回答としてマーク y-ske 2015年10月7日 3:03
    2015年10月6日 10:54
    モデレータ

すべての返信

  • 直接的な回答ではありませんが、
    BitmapTransformerに関する情報としては、
    画像を編集する方法
    https://msdn.microsoft.com/ja-jp/library/windows/apps/xaml/jj709937.aspx
    Simple imaging sample
    https://code.msdn.microsoft.com/windowsapps/Simple-Imaging-Sample-a2dec2b0/
    が、ございます。
    Simple imaging sampleを動かして、image transformをご確認いただければ、目的の
    回転操作ができることを確認することができます。
    このサンプルでは、左90度、右90度の回転や、上書き保存、名前を付けての保存を
    行うことができます。
    Image transformのサンプル コードを確認するには、サンプル内の
    SampleImaging.SharedプロジェクトのScenario2_ImageTransform.xaml.csを参照
    してください。
    上書き保存はSave_Clickメソッド、名前を付けて保存はSaveAs_Clickメソッドで
    実装されています。コードを参照される場合は、条件コンパイル変数の
    WINDOWS_PHONE_APPを満たさないコードがWindows 8.1になる点にご注意ください。

    2015年10月6日 9:37
  • > bytes = pixelData.DetachPixelData();

    の時点で、「画像の回転,拡大縮小を行い、回転、拡大縮小後のバイト配列」は手に入っていると思うのですが…? 想定外の値が入っていたのでしょうか?

    2015年10月6日 9:57
  • こんにちは。

    どううまく行かないのですか?

    横長の話をされていたのでひとつ気になったのは、例えばClockwise90Degreesで90度の回転をさせた場合は
    ScaleWidth, ScaleHeight、WriteableBitmapのサイズとか色々サイズを調整すると思うのですが、その辺りは大丈夫ですか。

    //transform.ScaledWidth = width;      //★
    //transform.ScaledHeight = height;    //★
    transform.ScaledWidth = height;
    transform.ScaledHeight = width;
    
    //transform.Rotation = BitmapRotation.Clockwise180Degrees;
    transform.Rotation = BitmapRotation.Clockwise90Degrees;
    //transform.InterpolationMode = BitmapInterpolationMode.Fant;
    
    PixelDataProvider pixelData = await logoData.GetPixelDataAsync(
        BitmapPixelFormat.Bgra8,
        BitmapAlphaMode.Straight,
        transform,
        ExifOrientationMode.IgnoreExifOrientation,
        ColorManagementMode.ColorManageToSRgb);
    
    bytes = pixelData.DetachPixelData();
    
    //var bitmap = new WriteableBitmap((int)width, (int)height); ★
    var bitmap = new WriteableBitmap((int)height, (int)width);
    

    バイト配列自体はHongliangさんが仰っているとおり既に取得済みだと思います。

    • 回答としてマーク y-ske 2015年10月7日 3:03
    2015年10月6日 10:54
    モデレータ