BitmapSource copyPixels正确的参数

时间:2012-11-22 10:45:54

标签: c++ copy command-line-interface pixel bitmapsource

我使用的步幅会产生异常。我不知道步伐是正确的。输入图像是32位JPG。请告诉我什么值(我尝试了很多东西,但是它们产生异常或损坏的JPG)我应该投入:

enter image description here

array<System::Byte>^ pixels = gcnew array<System::Byte>(WHAT VALUE);        
bitmapSource->CopyPixels(pixels, WHAT VALUE, 0);

        // Jpg.cpp : Defines the entry point for the console application.
        //

        #include "stdafx.h"
        #include <iostream>
        #using <mscorlib.dll> //requires CLI
        using namespace System;
        using namespace System::IO;
        using namespace System::Windows::Media::Imaging;
        using namespace System::Windows::Media;
        using namespace System::Windows::Controls;
        using namespace std;
        [System::STAThread]
        int _tmain(int argc, _TCHAR* argv[])
        {


            // Open a Stream and decode a JPEG image
                Stream^ imageStreamSource = gcnew FileStream("C:/heart2.jpg", FileMode::Open, FileAccess::Read, FileShare::Read);

                JpegBitmapDecoder^ decoder = gcnew JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions::PreservePixelFormat, BitmapCacheOption::Default);
                BitmapSource^ bitmapSource = decoder->Frames[0];//< --mamy bitmape
                // Draw the Image
                Image^ myImage = gcnew Image();


                myImage->Source = bitmapSource;
                myImage->Stretch = Stretch::None;
                myImage->Margin = System::Windows::Thickness(20);


  int width = bitmapSource->PixelWidth;
        int height = bitmapSource->PixelHeight;
        int stride = (width* bitmapSource->Format.BitsPerPixel + 31)/32;
        array<System::Byte>^ pixels = gcnew array<System::Byte>(height * width * stride);
        bitmapSource->CopyPixels(pixels, stride, 0);


        int x;
                cin>>x;
            return 0;
        }

1 个答案:

答案 0 :(得分:0)

谷歌

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.bitmapdata.stride.aspx

步幅是单行像素(扫描线)的宽度,四舍五入到四字节边界。

因此,正确的值取决于图像中每个像素的位数。