Passing bitmap from C# to C++ via a struct












0















First, I've read the a few questions regarding this issue, the most helpful being:
Passing bitmap from c# to c++



I was unable to get the provided solutions to work as I kept getting an AccessViolationException.



What I'm attempting to do is pass bitmap data to an unmanaged c++ dll. To do this I created a struct which holds a pointer to the image data as well as its length. I'm using a struct as I plan on passing in multiple images (in a single call) to the unmanaged API.



What I implemented works but I have a feeling there is probably some serious drawbacks so I'm curious as to what those drawbacks could be.



My current solution uses a generic pointer to hold the image data. This of course would be a drawback as I lose type safety. Anyway here is the relevant code.




c++ dll




raw_image.h



struct raw_image
{
void* data;
int size;
};


alignment.cpp (exports)



ALIGNMENT_API void submit( raw_image& img )
{
cv::Mat mat = cv::imdecode( cv::_InputArray(
static_cast<uchar*>( img.data ), img.size ), cv::IMREAD_COLOR );
cv::imshow( "image", mat );
cv::waitKey( );
cv::destroyWindow( "image" );
}



C# dll




RawImage.cs



[StructLayout( LayoutKind.Sequential )]
internal unsafe struct RawImage
{
internal void* ImageData;
internal int Length;
}


Aligner.cs (import)



[DllImport( "alignment-vc141-mtd-x64.dll", CallingConvention = 
CallingConvention.Cdecl )]
static extern void submit( RawImage img );


And this is where I pass the image to the unmanaged API.



using( var bitmap = new Bitmap( "AlignmentCenter.jpg" ) )
using( var stream = new MemoryStream( ) )
{
bitmap.Save( stream, ImageFormat.Jpeg );
var source = stream.ToArray( );
fixed( void* ptr = source )
{
var raw = new RawImage
{
ImageData = ptr,
Length = source.Length
};
submit( raw );
}
}


Is what I'm doing unsafe? Am I copying more than I should?
One last thing, I know about EmguCv and I've used it in the past but I won't be using it here.










share|improve this question























  • If your code works, maybe you should ask your question here. I think there you can get more accurate answer.

    – vasily.sib
    Nov 20 '18 at 2:55











  • Since you have working code, this might be more on-topic at CodeReview. I'm not sure how the questions you've asked can be answered in a way that isn't simply opinion. Consider if you really just want a yes or no answer, or if you're looking for alternatives. This isn't really a yes or no answer sort of place.

    – Retired Ninja
    Nov 20 '18 at 2:56











  • @RetiredNinja Do you know if there is a quick way to migrate the question over to CodeReview? Or should I just close this question and cut and paste it over there manually?

    – WBuck
    Nov 20 '18 at 3:01
















0















First, I've read the a few questions regarding this issue, the most helpful being:
Passing bitmap from c# to c++



I was unable to get the provided solutions to work as I kept getting an AccessViolationException.



What I'm attempting to do is pass bitmap data to an unmanaged c++ dll. To do this I created a struct which holds a pointer to the image data as well as its length. I'm using a struct as I plan on passing in multiple images (in a single call) to the unmanaged API.



What I implemented works but I have a feeling there is probably some serious drawbacks so I'm curious as to what those drawbacks could be.



My current solution uses a generic pointer to hold the image data. This of course would be a drawback as I lose type safety. Anyway here is the relevant code.




c++ dll




raw_image.h



struct raw_image
{
void* data;
int size;
};


alignment.cpp (exports)



ALIGNMENT_API void submit( raw_image& img )
{
cv::Mat mat = cv::imdecode( cv::_InputArray(
static_cast<uchar*>( img.data ), img.size ), cv::IMREAD_COLOR );
cv::imshow( "image", mat );
cv::waitKey( );
cv::destroyWindow( "image" );
}



C# dll




RawImage.cs



[StructLayout( LayoutKind.Sequential )]
internal unsafe struct RawImage
{
internal void* ImageData;
internal int Length;
}


Aligner.cs (import)



[DllImport( "alignment-vc141-mtd-x64.dll", CallingConvention = 
CallingConvention.Cdecl )]
static extern void submit( RawImage img );


And this is where I pass the image to the unmanaged API.



using( var bitmap = new Bitmap( "AlignmentCenter.jpg" ) )
using( var stream = new MemoryStream( ) )
{
bitmap.Save( stream, ImageFormat.Jpeg );
var source = stream.ToArray( );
fixed( void* ptr = source )
{
var raw = new RawImage
{
ImageData = ptr,
Length = source.Length
};
submit( raw );
}
}


Is what I'm doing unsafe? Am I copying more than I should?
One last thing, I know about EmguCv and I've used it in the past but I won't be using it here.










share|improve this question























  • If your code works, maybe you should ask your question here. I think there you can get more accurate answer.

    – vasily.sib
    Nov 20 '18 at 2:55











  • Since you have working code, this might be more on-topic at CodeReview. I'm not sure how the questions you've asked can be answered in a way that isn't simply opinion. Consider if you really just want a yes or no answer, or if you're looking for alternatives. This isn't really a yes or no answer sort of place.

    – Retired Ninja
    Nov 20 '18 at 2:56











  • @RetiredNinja Do you know if there is a quick way to migrate the question over to CodeReview? Or should I just close this question and cut and paste it over there manually?

    – WBuck
    Nov 20 '18 at 3:01














0












0








0








First, I've read the a few questions regarding this issue, the most helpful being:
Passing bitmap from c# to c++



I was unable to get the provided solutions to work as I kept getting an AccessViolationException.



What I'm attempting to do is pass bitmap data to an unmanaged c++ dll. To do this I created a struct which holds a pointer to the image data as well as its length. I'm using a struct as I plan on passing in multiple images (in a single call) to the unmanaged API.



What I implemented works but I have a feeling there is probably some serious drawbacks so I'm curious as to what those drawbacks could be.



My current solution uses a generic pointer to hold the image data. This of course would be a drawback as I lose type safety. Anyway here is the relevant code.




c++ dll




raw_image.h



struct raw_image
{
void* data;
int size;
};


alignment.cpp (exports)



ALIGNMENT_API void submit( raw_image& img )
{
cv::Mat mat = cv::imdecode( cv::_InputArray(
static_cast<uchar*>( img.data ), img.size ), cv::IMREAD_COLOR );
cv::imshow( "image", mat );
cv::waitKey( );
cv::destroyWindow( "image" );
}



C# dll




RawImage.cs



[StructLayout( LayoutKind.Sequential )]
internal unsafe struct RawImage
{
internal void* ImageData;
internal int Length;
}


Aligner.cs (import)



[DllImport( "alignment-vc141-mtd-x64.dll", CallingConvention = 
CallingConvention.Cdecl )]
static extern void submit( RawImage img );


And this is where I pass the image to the unmanaged API.



using( var bitmap = new Bitmap( "AlignmentCenter.jpg" ) )
using( var stream = new MemoryStream( ) )
{
bitmap.Save( stream, ImageFormat.Jpeg );
var source = stream.ToArray( );
fixed( void* ptr = source )
{
var raw = new RawImage
{
ImageData = ptr,
Length = source.Length
};
submit( raw );
}
}


Is what I'm doing unsafe? Am I copying more than I should?
One last thing, I know about EmguCv and I've used it in the past but I won't be using it here.










share|improve this question














First, I've read the a few questions regarding this issue, the most helpful being:
Passing bitmap from c# to c++



I was unable to get the provided solutions to work as I kept getting an AccessViolationException.



What I'm attempting to do is pass bitmap data to an unmanaged c++ dll. To do this I created a struct which holds a pointer to the image data as well as its length. I'm using a struct as I plan on passing in multiple images (in a single call) to the unmanaged API.



What I implemented works but I have a feeling there is probably some serious drawbacks so I'm curious as to what those drawbacks could be.



My current solution uses a generic pointer to hold the image data. This of course would be a drawback as I lose type safety. Anyway here is the relevant code.




c++ dll




raw_image.h



struct raw_image
{
void* data;
int size;
};


alignment.cpp (exports)



ALIGNMENT_API void submit( raw_image& img )
{
cv::Mat mat = cv::imdecode( cv::_InputArray(
static_cast<uchar*>( img.data ), img.size ), cv::IMREAD_COLOR );
cv::imshow( "image", mat );
cv::waitKey( );
cv::destroyWindow( "image" );
}



C# dll




RawImage.cs



[StructLayout( LayoutKind.Sequential )]
internal unsafe struct RawImage
{
internal void* ImageData;
internal int Length;
}


Aligner.cs (import)



[DllImport( "alignment-vc141-mtd-x64.dll", CallingConvention = 
CallingConvention.Cdecl )]
static extern void submit( RawImage img );


And this is where I pass the image to the unmanaged API.



using( var bitmap = new Bitmap( "AlignmentCenter.jpg" ) )
using( var stream = new MemoryStream( ) )
{
bitmap.Save( stream, ImageFormat.Jpeg );
var source = stream.ToArray( );
fixed( void* ptr = source )
{
var raw = new RawImage
{
ImageData = ptr,
Length = source.Length
};
submit( raw );
}
}


Is what I'm doing unsafe? Am I copying more than I should?
One last thing, I know about EmguCv and I've used it in the past but I won't be using it here.







c# c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 2:36









WBuckWBuck

868714




868714













  • If your code works, maybe you should ask your question here. I think there you can get more accurate answer.

    – vasily.sib
    Nov 20 '18 at 2:55











  • Since you have working code, this might be more on-topic at CodeReview. I'm not sure how the questions you've asked can be answered in a way that isn't simply opinion. Consider if you really just want a yes or no answer, or if you're looking for alternatives. This isn't really a yes or no answer sort of place.

    – Retired Ninja
    Nov 20 '18 at 2:56











  • @RetiredNinja Do you know if there is a quick way to migrate the question over to CodeReview? Or should I just close this question and cut and paste it over there manually?

    – WBuck
    Nov 20 '18 at 3:01



















  • If your code works, maybe you should ask your question here. I think there you can get more accurate answer.

    – vasily.sib
    Nov 20 '18 at 2:55











  • Since you have working code, this might be more on-topic at CodeReview. I'm not sure how the questions you've asked can be answered in a way that isn't simply opinion. Consider if you really just want a yes or no answer, or if you're looking for alternatives. This isn't really a yes or no answer sort of place.

    – Retired Ninja
    Nov 20 '18 at 2:56











  • @RetiredNinja Do you know if there is a quick way to migrate the question over to CodeReview? Or should I just close this question and cut and paste it over there manually?

    – WBuck
    Nov 20 '18 at 3:01

















If your code works, maybe you should ask your question here. I think there you can get more accurate answer.

– vasily.sib
Nov 20 '18 at 2:55





If your code works, maybe you should ask your question here. I think there you can get more accurate answer.

– vasily.sib
Nov 20 '18 at 2:55













Since you have working code, this might be more on-topic at CodeReview. I'm not sure how the questions you've asked can be answered in a way that isn't simply opinion. Consider if you really just want a yes or no answer, or if you're looking for alternatives. This isn't really a yes or no answer sort of place.

– Retired Ninja
Nov 20 '18 at 2:56





Since you have working code, this might be more on-topic at CodeReview. I'm not sure how the questions you've asked can be answered in a way that isn't simply opinion. Consider if you really just want a yes or no answer, or if you're looking for alternatives. This isn't really a yes or no answer sort of place.

– Retired Ninja
Nov 20 '18 at 2:56













@RetiredNinja Do you know if there is a quick way to migrate the question over to CodeReview? Or should I just close this question and cut and paste it over there manually?

– WBuck
Nov 20 '18 at 3:01





@RetiredNinja Do you know if there is a quick way to migrate the question over to CodeReview? Or should I just close this question and cut and paste it over there manually?

– WBuck
Nov 20 '18 at 3:01












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385401%2fpassing-bitmap-from-c-sharp-to-c-via-a-struct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385401%2fpassing-bitmap-from-c-sharp-to-c-via-a-struct%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?