Converting byte array of 16 grey degrees into 256-grey-degree format












0















i am trying to get image from fingerprint scanner through c# application, but when transferring through UART, to quicken speed, only the upper 4 bits of the pixel is transferred (that is 16 grey degrees). And two adjacent pixels of the same row will form a byte before the transferring.



So converting that byte stream back to image i am using following code:-



MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
pictureBox2.Image = returnImage;


But it is giving me error as




"Parameter not valid"




for :



Image returnImage = Image.FromStream(ms);


I am getting around 500 bytes of data. Could any one please provide any solution so that image when uploaded to PC, the 16-grey-degree image can be extended to 256-grey-degree format i.e. 8-bit BMP format.










share|improve this question

























  • Well what format is the data in?

    – Jon Skeet
    Aug 6 '13 at 9:07











  • Image.FromStream or one of its siblings that deal with other sources actually expect a fully formatted image file, ie. a full Jpeg or PNG file, not just the raw pixel values. There's a lot more to an image file format than just the pixels. Now, decoding nibbles (4 bits) to bytes is one problem, but getting it into a Image object is another. Which one do you require help with? Both?

    – Lasse Vågsæther Karlsen
    Aug 6 '13 at 9:09













  • Additionally, around 500 bytes of data, consisting of 2 4-bit nibbles each, translates to around 1000 pixels. How big is the image? 40*25?

    – Lasse Vågsæther Karlsen
    Aug 6 '13 at 9:10
















0















i am trying to get image from fingerprint scanner through c# application, but when transferring through UART, to quicken speed, only the upper 4 bits of the pixel is transferred (that is 16 grey degrees). And two adjacent pixels of the same row will form a byte before the transferring.



So converting that byte stream back to image i am using following code:-



MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
pictureBox2.Image = returnImage;


But it is giving me error as




"Parameter not valid"




for :



Image returnImage = Image.FromStream(ms);


I am getting around 500 bytes of data. Could any one please provide any solution so that image when uploaded to PC, the 16-grey-degree image can be extended to 256-grey-degree format i.e. 8-bit BMP format.










share|improve this question

























  • Well what format is the data in?

    – Jon Skeet
    Aug 6 '13 at 9:07











  • Image.FromStream or one of its siblings that deal with other sources actually expect a fully formatted image file, ie. a full Jpeg or PNG file, not just the raw pixel values. There's a lot more to an image file format than just the pixels. Now, decoding nibbles (4 bits) to bytes is one problem, but getting it into a Image object is another. Which one do you require help with? Both?

    – Lasse Vågsæther Karlsen
    Aug 6 '13 at 9:09













  • Additionally, around 500 bytes of data, consisting of 2 4-bit nibbles each, translates to around 1000 pixels. How big is the image? 40*25?

    – Lasse Vågsæther Karlsen
    Aug 6 '13 at 9:10














0












0








0








i am trying to get image from fingerprint scanner through c# application, but when transferring through UART, to quicken speed, only the upper 4 bits of the pixel is transferred (that is 16 grey degrees). And two adjacent pixels of the same row will form a byte before the transferring.



So converting that byte stream back to image i am using following code:-



MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
pictureBox2.Image = returnImage;


But it is giving me error as




"Parameter not valid"




for :



Image returnImage = Image.FromStream(ms);


I am getting around 500 bytes of data. Could any one please provide any solution so that image when uploaded to PC, the 16-grey-degree image can be extended to 256-grey-degree format i.e. 8-bit BMP format.










share|improve this question
















i am trying to get image from fingerprint scanner through c# application, but when transferring through UART, to quicken speed, only the upper 4 bits of the pixel is transferred (that is 16 grey degrees). And two adjacent pixels of the same row will form a byte before the transferring.



So converting that byte stream back to image i am using following code:-



MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
pictureBox2.Image = returnImage;


But it is giving me error as




"Parameter not valid"




for :



Image returnImage = Image.FromStream(ms);


I am getting around 500 bytes of data. Could any one please provide any solution so that image when uploaded to PC, the 16-grey-degree image can be extended to 256-grey-degree format i.e. 8-bit BMP format.







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 6 '13 at 9:06









Soner Gönül

80.4k27148276




80.4k27148276










asked Aug 6 '13 at 9:05









sami manchndasami manchnda

16112




16112













  • Well what format is the data in?

    – Jon Skeet
    Aug 6 '13 at 9:07











  • Image.FromStream or one of its siblings that deal with other sources actually expect a fully formatted image file, ie. a full Jpeg or PNG file, not just the raw pixel values. There's a lot more to an image file format than just the pixels. Now, decoding nibbles (4 bits) to bytes is one problem, but getting it into a Image object is another. Which one do you require help with? Both?

    – Lasse Vågsæther Karlsen
    Aug 6 '13 at 9:09













  • Additionally, around 500 bytes of data, consisting of 2 4-bit nibbles each, translates to around 1000 pixels. How big is the image? 40*25?

    – Lasse Vågsæther Karlsen
    Aug 6 '13 at 9:10



















  • Well what format is the data in?

    – Jon Skeet
    Aug 6 '13 at 9:07











  • Image.FromStream or one of its siblings that deal with other sources actually expect a fully formatted image file, ie. a full Jpeg or PNG file, not just the raw pixel values. There's a lot more to an image file format than just the pixels. Now, decoding nibbles (4 bits) to bytes is one problem, but getting it into a Image object is another. Which one do you require help with? Both?

    – Lasse Vågsæther Karlsen
    Aug 6 '13 at 9:09













  • Additionally, around 500 bytes of data, consisting of 2 4-bit nibbles each, translates to around 1000 pixels. How big is the image? 40*25?

    – Lasse Vågsæther Karlsen
    Aug 6 '13 at 9:10

















Well what format is the data in?

– Jon Skeet
Aug 6 '13 at 9:07





Well what format is the data in?

– Jon Skeet
Aug 6 '13 at 9:07













Image.FromStream or one of its siblings that deal with other sources actually expect a fully formatted image file, ie. a full Jpeg or PNG file, not just the raw pixel values. There's a lot more to an image file format than just the pixels. Now, decoding nibbles (4 bits) to bytes is one problem, but getting it into a Image object is another. Which one do you require help with? Both?

– Lasse Vågsæther Karlsen
Aug 6 '13 at 9:09







Image.FromStream or one of its siblings that deal with other sources actually expect a fully formatted image file, ie. a full Jpeg or PNG file, not just the raw pixel values. There's a lot more to an image file format than just the pixels. Now, decoding nibbles (4 bits) to bytes is one problem, but getting it into a Image object is another. Which one do you require help with? Both?

– Lasse Vågsæther Karlsen
Aug 6 '13 at 9:09















Additionally, around 500 bytes of data, consisting of 2 4-bit nibbles each, translates to around 1000 pixels. How big is the image? 40*25?

– Lasse Vågsæther Karlsen
Aug 6 '13 at 9:10





Additionally, around 500 bytes of data, consisting of 2 4-bit nibbles each, translates to around 1000 pixels. How big is the image? 40*25?

– Lasse Vågsæther Karlsen
Aug 6 '13 at 9:10












1 Answer
1






active

oldest

votes


















1














You could transform the nibbles into bytes and then use the transformed data, like so:



public byte NibblesToBytes(byte data)
{
byte result = new byte[data.Length*2];
int i = 0;

foreach (var bits in data)
{
// You may need to reverse these two lines, depending on data format:
result[i++] = (byte)((bits & 0x0F) << 4);
result[i++] = (byte)(bits & 0xF0);
}

return result;
}


Those bytes will be greyscale values. To convert each value to an RGB value, you would need to set each of the R, G and B values to the greyscale value (I expect that you already know that, but I mention it just in case).



But you will need to do more than just this. You will need header information and so on.



These threads might help with that:



Create Bitmap from a byte array of pixel data



Convert byte Array or File Storage to Bitmap Image






share|improve this answer


























  • Thanks a lot for the help

    – sami manchnda
    Aug 6 '13 at 10:45











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%2f18075907%2fconverting-byte-array-of-16-grey-degrees-into-256-grey-degree-format%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You could transform the nibbles into bytes and then use the transformed data, like so:



public byte NibblesToBytes(byte data)
{
byte result = new byte[data.Length*2];
int i = 0;

foreach (var bits in data)
{
// You may need to reverse these two lines, depending on data format:
result[i++] = (byte)((bits & 0x0F) << 4);
result[i++] = (byte)(bits & 0xF0);
}

return result;
}


Those bytes will be greyscale values. To convert each value to an RGB value, you would need to set each of the R, G and B values to the greyscale value (I expect that you already know that, but I mention it just in case).



But you will need to do more than just this. You will need header information and so on.



These threads might help with that:



Create Bitmap from a byte array of pixel data



Convert byte Array or File Storage to Bitmap Image






share|improve this answer


























  • Thanks a lot for the help

    – sami manchnda
    Aug 6 '13 at 10:45
















1














You could transform the nibbles into bytes and then use the transformed data, like so:



public byte NibblesToBytes(byte data)
{
byte result = new byte[data.Length*2];
int i = 0;

foreach (var bits in data)
{
// You may need to reverse these two lines, depending on data format:
result[i++] = (byte)((bits & 0x0F) << 4);
result[i++] = (byte)(bits & 0xF0);
}

return result;
}


Those bytes will be greyscale values. To convert each value to an RGB value, you would need to set each of the R, G and B values to the greyscale value (I expect that you already know that, but I mention it just in case).



But you will need to do more than just this. You will need header information and so on.



These threads might help with that:



Create Bitmap from a byte array of pixel data



Convert byte Array or File Storage to Bitmap Image






share|improve this answer


























  • Thanks a lot for the help

    – sami manchnda
    Aug 6 '13 at 10:45














1












1








1







You could transform the nibbles into bytes and then use the transformed data, like so:



public byte NibblesToBytes(byte data)
{
byte result = new byte[data.Length*2];
int i = 0;

foreach (var bits in data)
{
// You may need to reverse these two lines, depending on data format:
result[i++] = (byte)((bits & 0x0F) << 4);
result[i++] = (byte)(bits & 0xF0);
}

return result;
}


Those bytes will be greyscale values. To convert each value to an RGB value, you would need to set each of the R, G and B values to the greyscale value (I expect that you already know that, but I mention it just in case).



But you will need to do more than just this. You will need header information and so on.



These threads might help with that:



Create Bitmap from a byte array of pixel data



Convert byte Array or File Storage to Bitmap Image






share|improve this answer















You could transform the nibbles into bytes and then use the transformed data, like so:



public byte NibblesToBytes(byte data)
{
byte result = new byte[data.Length*2];
int i = 0;

foreach (var bits in data)
{
// You may need to reverse these two lines, depending on data format:
result[i++] = (byte)((bits & 0x0F) << 4);
result[i++] = (byte)(bits & 0xF0);
}

return result;
}


Those bytes will be greyscale values. To convert each value to an RGB value, you would need to set each of the R, G and B values to the greyscale value (I expect that you already know that, but I mention it just in case).



But you will need to do more than just this. You will need header information and so on.



These threads might help with that:



Create Bitmap from a byte array of pixel data



Convert byte Array or File Storage to Bitmap Image







share|improve this answer














share|improve this answer



share|improve this answer








edited May 23 '17 at 12:11









Community

11




11










answered Aug 6 '13 at 9:11









Matthew WatsonMatthew Watson

73k692178




73k692178













  • Thanks a lot for the help

    – sami manchnda
    Aug 6 '13 at 10:45



















  • Thanks a lot for the help

    – sami manchnda
    Aug 6 '13 at 10:45

















Thanks a lot for the help

– sami manchnda
Aug 6 '13 at 10:45





Thanks a lot for the help

– sami manchnda
Aug 6 '13 at 10:45




















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%2f18075907%2fconverting-byte-array-of-16-grey-degrees-into-256-grey-degree-format%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

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks