public void Rotate90AgainClockwise() { uint height = ChangedImg.BM_Height; uint width = ChangedImg.BM_Width; ChangedImg.BM_Width = height; //change Width in buffer array ChangedImg.SetWidthTObuff(height); ChangedImg.BM_Height = width; //change Height in buffer array ChangedImg.SetHeightTObuff(width); ChangedImg.RowLength = Convert.ToUInt32(Math.Ceiling(ChangedImg.BM_Width * ChangedImg.BM_BitsPerPixel / 32.0) * 4); //number of bytes on the row ChangedImg.RowBitAlignment = (ChangedImg.RowLength * 8) - (ChangedImg.BM_Width * ChangedImg.BM_BitsPerPixel); //number of bits used for aligment of row ChangedImg.RowByteAlignment = ChangedImg.RowBitAlignment / 8; uint NewBuffLength = ChangedImg.BM_Offset + (ChangedImg.RowLength) * ChangedImg.BM_Height; byte[] buffResized = new byte[NewBuffLength]; if (ChangedImg.BM_BitsPerPixel == 24) { VColor[,] pixelArrDest = new VColor[ChangedImg.BM_Height, ChangedImg.BM_Width]; for (int r = 0; r < ChangedImg.BM_Width; r++) { for (int c = 0; c < ChangedImg.BM_Height; c++) { pixelArrDest[c, r] = ChangedImg.pixelArr[r, ChangedImg.BM_Height - 1 - c]; } } //set new created pixelArr ChangedImg.pixelArr = pixelArrDest; } if ((ChangedImg.BM_BitsPerPixel == 8) || (ChangedImg.BM_BitsPerPixel == 1)) { uint[,] pixelIndexArrDest = new uint[ChangedImg.BM_Height, ChangedImg.BM_Width]; for (int r = 0; r < ChangedImg.BM_Width; r++) { for (int c = 0; c < ChangedImg.BM_Height; c++) { pixelIndexArrDest[c, r] = ChangedImg.pixelIndexArr[r, ChangedImg.BM_Height - 1 - c]; } } //set new created pixelArr ChangedImg.pixelIndexArr = pixelIndexArrDest; } //copy fist 52 bytes to Array.Copy(ChangedImg.buff, 0, buffResized, 0, ChangedImg.BM_Offset); ChangedImg.buff = buffResized; //change BM_Size in buffer array ChangedImg.BM_Size = NewBuffLength; ChangedImg.SetSizeTObuff(ChangedImg.BM_Size); ChangedImg.SavePictureToFile(PathToChangedPicture); //after all ¨changes ubdate LoadImg by ChangedImg BitMap ClonedChangedImg = (BitMap)ChangedImg.Clone(); LoadedImg = ClonedChangedImg; }
public BitMap(String fileName) { FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); streamBits = new BinaryReader(fs); long numbytes = new FileInfo(fileName).Length; buff = new byte[(int)numbytes]; buff = streamBits.ReadBytes((int)numbytes); streamBits.BaseStream.Seek(0, SeekOrigin.Begin); //streamBits.Close; ukoncení //je možno použít buď Convert.ToUInt32() nebo (uint) BM_Type = (uint)(streamBits.ReadInt16()); BM_Size = Convert.ToUInt32(streamBits.ReadInt32()); Unused1 = (uint)(streamBits.ReadInt16()); Unused2 = (uint)(streamBits.ReadInt16()); BM_Offset = (uint)(streamBits.ReadInt32()); //BITMAPINFOHEADER \/ BM_NumberOfBit = (uint)(streamBits.ReadInt32()); BM_Width = (uint)(streamBits.ReadInt32()); BM_Height = (uint)(streamBits.ReadInt32()); BM_Planes = (uint)(streamBits.ReadInt16()); BM_BitsPerPixel = (uint)(streamBits.ReadInt16()); //2B-biBitCount BM_Compression = (uint)(streamBits.ReadInt32()); //4B-biCompression (neřešíme) BM_ByteSizeToCom = (uint)(streamBits.ReadInt32()); //4B-biSizeImage (asi nepotřebujeme) BM_XOutPerMeter = (uint)(streamBits.ReadInt32()); //4B-biXPelsPerMeter BM_YOutPerMeter = (uint)(streamBits.ReadInt32()); //4B-biYPelsPerMeter BM_ByteColorUsed = (uint)(streamBits.ReadInt32()); //4B-biClrUsed BM_NeededByteToColor = (uint)(streamBits.ReadInt32()); //4B-biClrImportant RowLength = Convert.ToUInt32(Math.Ceiling(BM_Width * BM_BitsPerPixel / 32.0) * 4); //number of bytes on row RowBitAlignment = (RowLength * 8) - (BM_Width * BM_BitsPerPixel); //number of bits used for aligment of row RowByteAlignment = RowBitAlignment / 8; //number of byts used for aligment of row pixelArr = new VColor[BM_Height, BM_Width]; //array with indexes of R,G,B, mainy used for 24bit picture SizeOfPallet = BM_Offset - HeadersLength; ColorPalette = new VColor[SizeOfPallet / 4]; //4 because every color is expressed by 4 BYTES pixelIndexArr = new uint[BM_Height, BM_Width]; //pole pixelů, jednotlivé složky plole mají v sobě uloženy indexi do pole barevné palety ColorPalette //----------------------------loading of picture 1 bit if (BM_BitsPerPixel == 1) { //loading of color palette 1 bit picture for (int col = 0; col < (SizeOfPallet / 4); col++) { TempIndex = HeadersLength + col * 4 + 0; blue = buff[TempIndex]; green = buff[HeadersLength + col * 4 + 1]; TempIndex3 = HeadersLength + col * 4 + 2; red = buff[TempIndex3]; ColorPalette[col] = new VColor(red, green, blue); } //loading of pixel array 1BIT picture, true = white, false=black BM_OffsetMoved = BM_Offset; BitArray bits; byte[] arr = new byte[RowLength]; for (int r = 0; r < BM_Height; r++) { Array.Copy(buff, BM_OffsetMoved, arr, 0, RowLength); int indexArrayOffest = 1; bits = new BitArray(arr); for (int col = 0; col < BM_Width; col++) { //if je kvůli posunu na další osmici bitů po přečtení první osmice bitů if (((col % 8) == 0) && col != 0) { indexArrayOffest++; } int Index = indexArrayOffest * 8 - col - 1 + ((indexArrayOffest - 1) * 8); pixelIndexArr[r, col] = Convert.ToUInt32(bits[Index]); } //before every iteratyion of "r"need to increment Offset BM_OffsetMoved = BM_OffsetMoved + RowLength; } //due to generalization, is created pixelArr for 1Bit picture for (int r = 0; r < BM_Height; r++) { for (int col = 0; col < BM_Width; col++) { uint indexInColorPalette = pixelIndexArr[r, col]; pixelArr[r, col] = new VColor( ColorPalette[indexInColorPalette].R, ColorPalette[indexInColorPalette].G, ColorPalette[indexInColorPalette].B); } //after first iteration we have to change Offset BM_OffsetMoved = Convert.ToUInt32(TempIndex3) + RowByteAlignment + 1; } } //------------------------------loading of fixel array 8 bit picture if (BM_BitsPerPixel == 8) { //loading of picture palette for (int col = 0; col < (SizeOfPallet / 4); col++) { TempIndex = HeadersLength + col * 4 + 0; blue = buff[TempIndex]; green = buff[HeadersLength + col * 4 + 1]; TempIndex3 = HeadersLength + col * 4 + 2; red = buff[TempIndex3]; ColorPalette[col] = new VColor(red, green, blue); } //loading of pixel array 8 bit picture BM_OffsetMoved = BM_Offset; for (int r = 0; r < BM_Height; r++) { for (int col = 0; col < BM_Width; col++) { TempIndex = BM_OffsetMoved + col; UInt32 ValueIndex = buff[TempIndex]; pixelIndexArr[r, col] = ValueIndex; } //after first iteration we have to change Offset BM_OffsetMoved = Convert.ToUInt32(TempIndex) + RowByteAlignment + 1; } //due to generalization, is created pixelArr for 8Bit picture - but it is not used in applicaton for (int r = 0; r < BM_Height; r++) { for (int col = 0; col < BM_Width; col++) { uint indexInColorPalette = pixelIndexArr[r, col]; pixelArr[r, col] = new VColor( ColorPalette[indexInColorPalette].R, ColorPalette[indexInColorPalette].G, ColorPalette[indexInColorPalette].B); } //after first iteration we have to change Offset BM_OffsetMoved = Convert.ToUInt32(TempIndex3) + RowByteAlignment + 1; } } //------------------------------loading of fixel array 24 bit picture if (BM_BitsPerPixel == 24) { BM_OffsetMoved = BM_Offset; for (int r = 0; r < BM_Height; r++) { for (int col = 0; col < BM_Width; col++) { TempIndex = BM_OffsetMoved + 3 * col + 0; //b blue = buff[BM_OffsetMoved + 3 * col + 0]; //g TempIndex2 = BM_OffsetMoved + 3 * col + 1; green = buff[BM_OffsetMoved + 3 * col + 1]; //r TempIndex3 = BM_OffsetMoved + 3 * col + 2; red = buff[BM_OffsetMoved + 3 * col + 2]; pixelArr[r, col] = new VColor(red, green, blue); } //after first iteration we have to change Offset BM_OffsetMoved = Convert.ToUInt32(TempIndex3) + RowByteAlignment + 1; } } //uvolnění zdrojů fs.Close(); }