-2

I want to convert bitmap image to ushort?[] type. I am following this link but it does not serve the purpose as I dont require byte array.

Please help.

Solution

var shorts = Array.ConvertAll(croppedImageByteArray, b => (ushort?)b);
Community
  • 1
  • 1
Beelal Ahmed
  • 51
  • 1
  • 8
  • What do you want in the shorts - a 16-bit representation of the colour in each cell, or the byte representation of the image in some format (BMP, PNG, JPG) extended to shorts? And why nullable? – Rup May 18 '16 at 11:22
  • Actually I will hit one WSDL webservice, which requires me to send image in this form. – Beelal Ahmed May 18 '16 at 11:25
  • 16 bit is high color mode. If you an application requires 16 bit you have to convert the 8 bit images before send to 16 bit format. You can't just cast a 8 bit image to 16 bit. – jdweng May 18 '16 at 11:42

1 Answers1

0

Follow the same link and cast the bytes to ushort? On the final result where you have the byte array you can do

var WhatIWant = TheByteArray.Cast<ushort?>();

It's only useful if you specifically need that type, if you want to convert up from byte to a larger type to get more information that's a no go as that information doesn't exist to begin with if it's stored as bytes from the start.

If this doesn't help explain clearly what you are trying to do and most of all why so we can better help.

Ronan Thibaudau
  • 3,413
  • 3
  • 29
  • 78
  • It does not convert byte array to ushort?[]. – Beelal Ahmed May 18 '16 at 11:25
  • @BeelalAhmed It does, it's exactly what it does, but i doubt it is what you want which is why i said explain clearly WHY you want to do that – Ronan Thibaudau May 18 '16 at 11:26
  • I see you added the answer to that as a comment on the original post, in that case yes it will work (it will be a ushort array) but i doubt that the service just wants any ushort array, refer to their documentation but i doubt they just want the bitmap casted to ushorts – Ronan Thibaudau May 18 '16 at 11:27
  • AFAICS this will generate an enumerable of ushorts (not nullable ushorts), but that can probably be fixed with a Select and a ToArray. – Rup May 18 '16 at 11:28
  • I didn't see he wanted them nullable, it doesn't require a select, can just fix the cast signature, doing it now – Ronan Thibaudau May 18 '16 at 11:29
  • This converts to `ushort[]`, the question (inexplicably) calls for `ushort?[]`. – phoog May 18 '16 at 11:30
  • I am getting compile time error of non conversion. Cannot implicitly convert type from IEnumerable to ushort?[] – Beelal Ahmed May 18 '16 at 11:34
  • croppedImageByteArray.Cast().ToArray() is givnig error. 'An unhandled exception of type 'System.InvalidCastException' occurred in System.Core.dll' – Beelal Ahmed May 18 '16 at 11:47
  • I solved it with it. var shorts = Array.ConvertAll(croppedImageByteArray, b => (ushort?)b); – Beelal Ahmed May 18 '16 at 12:26
  • The compile time error just Required a ToArray since you want an array – Ronan Thibaudau May 18 '16 at 14:19