0

Used stride by me generates exception. I don't know what stride is correct. The input image is 32 bit JPG. Please tell me what values(I tried many things but they where generating exceptions or corrupted JPG) i should put into:

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;
}
Chris O
  • 5,017
  • 3
  • 35
  • 42

1 Answers1

1

Google

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

The stride is the width of a single row of pixels (a scan line), rounded up to a four-byte boundary.

So the correct value depends on how many bits per pixel you have in your image.

john
  • 7,897
  • 29
  • 27
  • Again using google (I'm not an expert) it looks like `bitmapSource->Format->BitsPerPixel`. – john Nov 22 '12 at 11:46
  • So the stride calculation would be `stride = (width*bitmapSource->Format->BitsPerPixel + 31)/32;` – john Nov 22 '12 at 11:48
  • Got that what is in edit. Don't know why. I have also edited code. –  Nov 22 '12 at 12:17
  • 1
    `array^ pixels = gcnew array(height * width * stride);` is way too much memory, `array^ pixels = gcnew array(height * stride);` – john Nov 22 '12 at 12:35