/// <summary> /// Used to create a bitmap from provieded bytes /// </summary> /// <param name="rowColors">Contains bytes of bitmap</param> /// <returns></returns> private Bitmap CreateBM(ColorObject[][] rowColors) { int width = rowColors[0].Count(); int height = rowColors.Count(); //int width = rowColors.Values.Where(o => o == 0).Count(); Bitmap bitm = new Bitmap(width, height, PixelFormat.Format24bppRgb); // new Bitmap(imgdat.GetUpperBound(1) + 1, imgdat.GetUpperBound(0) + 1, PixelFormat.Format24bppRgb); BitmapData bitmapdat = bitm.LockBits(new Rectangle(0, 0, bitm.Width, bitm.Height), ImageLockMode.ReadWrite, bitm.PixelFormat); int stride = bitmapdat.Stride; byte[] bytes = new byte[stride * bitm.Height]; for (int r = 0; r < bitm.Height; r++) { for (int c = 0; c < bitm.Width; c++) { ColorObject color = rowColors[r][c]; bytes[(r * stride) + c * 3] = color.Blue; bytes[(r * stride) + c * 3 + 1] = color.Green; bytes[(r * stride) + c * 3 + 2] = color.Red; } } System.IntPtr scan0 = bitmapdat.Scan0; Marshal.Copy(bytes, 0, scan0, stride * bitm.Height); bitm.UnlockBits(bitmapdat); return(bitm); }
/// <summary> /// It opens the image using memory mapped view and read the needed /// parts, then call CreateBM to create a partially bitmap /// </summary> /// <param name="bmpFilename">Path to the physical bitmap</param> /// <returns></returns> public Bitmap readPartOfImage(string bmpFilename) { var headers = ReadHeaders(bmpFilename); var mmf = MemoryMappedFile.CreateFromFile(bmpFilename, FileMode.Open); int rowSize = headers.Item2.RowSize; // number of byes in a row // Dictionary<ColorObject, int> rowColors = new Dictionary<ColorObject, int>(); int colorSize = Marshal.SizeOf(typeof(MyColor)); int width = rowSize / colorSize;//(headers.Item1.DataOffset+ rowSize) / colorSize; int height = 200; ColorObject cObj; MyColor outObj; ColorObject[][] rowColors = new ColorObject[height][]; // Read the view image and save row by row pixel for (int j = 0; j < height; j++) { rowColors[j] = new ColorObject[width]; using (var view = mmf.CreateViewAccessor(headers.Item1.DataOffset + rowSize * j, rowSize, MemoryMappedFileAccess.Read)) { for (long i = 0; i < rowSize; i += colorSize) { view.Read(i, out outObj); cObj = new ColorObject(outObj); rowColors[j][i / colorSize] = cObj; } } } return(CreateBM(rowColors)); }