/// <summary> /// Converts <see cref="HSD_TOBJ"/> into <see cref="Bitmap"/> /// </summary> /// <param name="tobj"></param> /// <returns></returns> public static Bitmap ToBitmap(HSD_TOBJ tobj) { if (tobj.ImageData == null) { return(null); } var rgba = tobj.GetDecodedImageData(); return(BitmapTools.BGRAToBitmap(rgba, tobj.ImageData.Width, tobj.ImageData.Height)); }
/// <summary> /// /// </summary> /// <param name="path"></param> /// <returns></returns> public static Bitmap LoadBitmapFromFile(string path) { // check for alpha channel bitmap and manually import it if (Path.GetExtension(path).ToLower().Equals(".bmp")) { using (FileStream stream = new FileStream(path, FileMode.Open)) using (BinaryReader r = new BinaryReader(stream)) { if (new string(r.ReadChars(2)) == "BM") { var fSize = r.ReadUInt32(); r.ReadInt32(); // reserved var dataOffset = r.ReadUInt32(); var infoSize = r.ReadUInt32(); var width = r.ReadInt32(); var height = r.ReadInt32(); var planes = r.ReadInt16(); var bpp = r.ReadInt16(); // only do this if this image is 32 bpp if (bpp == 32) { var comp = r.ReadInt32(); if (comp != 0) { throw new NotSupportedException("Compressed 32 bpp bitmap not supported"); } var imageSize = r.ReadInt32(); r.BaseStream.Position = dataOffset; var data = r.ReadBytes(imageSize); var scan = new byte[data.Length]; for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++) { var i = w + h * width; var d = w + (height - 1 - h) * width; i *= 4; d *= 4; scan[i] = data[d]; scan[i + 1] = data[d + 1]; scan[i + 2] = data[d + 2]; scan[i + 3] = data[d + 3]; } } return(BitmapTools.BGRAToBitmap(scan, width, height)); } } } } // otherwise use dotnet's return(new Bitmap(path)); }