public bool SetPixelData(Image currimage)
{
this._modulestring = "SetPixelData";
Bitmap bmp = (Bitmap)currimage;
try
{
if (bmp == null)
{
this.LogErrorString("invalid image sent to the function");
return false;
}
byte[] rawbytesfrombmp = GetRawBytesFromBmp(bmp);//ImageToByteArray(currimage);//GetRawBytesFromBmp(bmp);
if (rawbytesfrombmp == null)
{
this.LogErrorString("unable to get raw bytes from BMP image");
return false;
}
if (((bmp.PixelFormat == PixelFormat.Format24bppRgb) || (bmp.PixelFormat == PixelFormat.Format32bppArgb)) || (bmp.PixelFormat == PixelFormat.Format32bppRgb))
{
ushort height = (ushort)bmp.Height;
ushort width = (ushort)bmp.Width;
if (((height % 2) != 0) && ((width % 2) != 0))
{
width = (ushort)(width - 1);
}
////---------------------------------
if ((bmp.PixelFormat == PixelFormat.Format24bppRgb) || (bmp.PixelFormat == PixelFormat.Format32bppArgb) || (bmp.PixelFormat == PixelFormat.Format32bppRgb))
{
//int frameNum = 0;
int frameNum = 0;
//DicomCompressedPixelData fragments = new DicomCompressedPixelData(_dcmfile.DataSet, rawbytesfrombmp);
//DicomCompressedPixelData fragments = new DicomCompressedPixelData(_dcmfile);
DicomCompressedPixelData fragments = new DicomCompressedPixelData(_dcmfile, rawbytesfrombmp);
fragments.ImageWidth = width;//(ushort)height;
fragments.ImageHeight = height; //(ushort)image.Height;
fragments.BitsStored = 8;//(ushort)bitsPerPixel;
fragments.BitsAllocated = 8;//(ushort)bitsPerPixel;
fragments.HighBit = 7;
fragments.SamplesPerPixel = 3;
fragments.PlanarConfiguration = 0;
fragments.PhotometricInterpretation = "YBR_FULL_422";
fragments.AddFrameFragment(rawbytesfrombmp);///addtional added
fragments.UpdateMessage(_dcmfile); ///
//byte[] frame = fragments.GetFrameFragmentData(frameNum);
//-----------------------------------------------
//DicomCompressedPixelData fragments = new DicomCompressedPixelData(file.DataSet);
//byte[] frame = fragments.GetFrameFragmentData(frameNum);
//--------------------------------End
//if (!this.SetPixelData8bit(frame))///(!this.SetPixelData8bit(rawbytesfrombmp)) //if (!this.SetPixelData8bitDicomSequence(ref seq))
//{
// return false;
//}
}
}
}
catch (Exception exception)
{
this.LogErrorString(exception.Message);
return false;
}
return true;
}
public static byte[] ImageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
private byte[] GetRawBytesFromBmp(Bitmap bmp)
{
byte[] raw;
int row, column;
int width = bmp.Width;
int columnWidth = width / 4;
int height = bmp.Height;
if (height % 2 != 0 && width % 2 != 0)
{
width--;
}
int bmpStride = 0;
int rawStride = 0;
int BytesPerPixel = 0;
BitmapData data = null;
try
{
//Lock the bitmap so we can access the raw pixel data
// Lock the bitmap's bits.
data = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, bmp.PixelFormat);
bmpStride = data.Stride;
if (bmp.PixelFormat == PixelFormat.Format24bppRgb || bmp.PixelFormat == PixelFormat.Format8bppIndexed || bmp.PixelFormat == PixelFormat.Format32bppRgb || bmp.PixelFormat == PixelFormat.Format32bppArgb)
{
BytesPerPixel = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
rawStride = BytesPerPixel * width;
}
else if (bmp.PixelFormat == PixelFormat.Format48bppRgb)
{
BytesPerPixel = Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
rawStride = BytesPerPixel * width;
}
else
{
return null;
}
// Declare an array to hold the bytes of the bitmap.
raw = new byte[rawStride * height];
unsafe
{
// Get the address of the first line. //Sets pointer
byte* bmpPtr = (byte*)data.Scan0.ToPointer();
fixed (byte* rawPtr = raw)
if (bmp.PixelFormat == PixelFormat.Format24bppRgb)
{
//the pixels row by row
for (row = 0; row < height; ++row)
{
for (column = 0; column < rawStride; column += BytesPerPixel)
{
rawPtr[row * rawStride + column] = bmpPtr[row * bmpStride + column + 2]; //b
rawPtr[row * rawStride + column + 1] = bmpPtr[row * bmpStride + column + 1]; //g
rawPtr[row * rawStride + column + 2] = bmpPtr[row * bmpStride + column]; ; //r
}
}
}
}
}
catch (Exception e)
{
return null;
}
finally
{
bmp.UnlockBits(data);
}
return raw;
}