//------------------------------------------------------------------------------------------------ //Создание изображения из матриц R G B public static WriteableBitmap CreateWriteableBitmapFromMatricesRGB( IntegerMatrix redMatrix, IntegerMatrix greenMatrix, IntegerMatrix blueMatrix, int dpiX, int dpiY ) { int width = redMatrix.ColumnCount; int height = redMatrix.RowCount; PixelFormat pixelFormat = PixelFormats.Bgra32; WriteableBitmap newImage = WriteableBitmapCreator.CreateWriteableBitmap (width, height, dpiX, dpiY, pixelFormat); WriteableBitmapWrapper wrapper = WriteableBitmapWrapper.Create(newImage); byte[] pixelBytes = new byte[4 * width * height]; //BGRA int pixelByteIndex = 0; for (int y = 0; y < newImage.PixelHeight; y++) { for (int x = 0; x < newImage.PixelWidth; x++) { pixelBytes[pixelByteIndex++] = Convert.ToByte(blueMatrix[y, x]); pixelBytes[pixelByteIndex++] = Convert.ToByte(greenMatrix[y, x]); pixelBytes[pixelByteIndex++] = Convert.ToByte(redMatrix[y, x]); pixelBytes[pixelByteIndex++] = byte.MaxValue; } } wrapper.WritePixels(pixelBytes); return(newImage); }
//------------------------------------------------------------------------------------------------ //Создание изображения из матрицы интенсивностей серого по шаблону public static WriteableBitmap CreateGrayScaleWriteableBitmapFromMatrix( RealMatrix grayScaleMatrix, int dpiX, int dpiY, BitMask2D bitMask ) { int pixelWidth = grayScaleMatrix.ColumnCount; int pixelHeight = grayScaleMatrix.RowCount; WriteableBitmap writeableBitmap = WriteableBitmapCreator.CreateWriteableBitmap (pixelWidth, pixelHeight, dpiX, dpiY, PixelFormats.Bgra32); WriteableBitmapWrapper bitmapWrapper = WriteableBitmapWrapper.Create(writeableBitmap); for (int x = 0; x < pixelWidth; x++) { for (int y = 0; y < pixelHeight; y++) { if (bitMask[y, x] == true) { int grayIntensity = ( int )Math.Round(grayScaleMatrix[y, x]); byte red, green, blue; red = green = blue = Convert.ToByte(grayIntensity); Color color = Color.FromRgb(red, green, blue); bitmapWrapper.SetPixelColor(x, y, color); } } } return(writeableBitmap); }
//----------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------- //Новое изображение c заданными dpiX, dpiY public WriteableBitmap GetNewWriteableBitmap(int dpiX, int dpiY) { int pixelWidth = this.Image.PixelWidth; int pixelHeight = this.Image.PixelHeight; int stride = this.GetStride(); Int32Rect rect = new Int32Rect(0, 0, pixelWidth, pixelHeight); byte[] pixelsBytes = new byte[this.SizeInBytes]; this.Image.CopyPixels(rect, pixelsBytes, stride, 0); media.PixelFormat pixelFormat = this.Image.Format; WriteableBitmap newImage = WriteableBitmapCreator.CreateWriteableBitmap (pixelWidth, pixelHeight, dpiX, dpiY, pixelFormat); newImage.WritePixels(rect, pixelsBytes, stride, 0); return(newImage); }
//----------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------- //Обрезка изображения public WriteableBitmap GetSubBitmap(System.Drawing.Point leftTop, System.Drawing.Point rightBottom) { int newImageWidth = Convert.ToInt32(Math.Abs(rightBottom.X - leftTop.X)) + 1; int newImageHeight = Convert.ToInt32(Math.Abs(rightBottom.Y - leftTop.Y)) + 1; media.PixelFormat pixelFormat = this.Image.Format; int dpiX = Convert.ToInt32(OS.OS.SystemDpiX); int dpiY = Convert.ToInt32(OS.OS.SystemDpiY); WriteableBitmap newBitmap = WriteableBitmapCreator.CreateWriteableBitmap (newImageWidth, newImageHeight, dpiX, dpiY, pixelFormat); WriteableBitmapWrapper newImageWrapper = WriteableBitmapWrapper.Create(newBitmap); for (int y = leftTop.Y, newY = 0; y <= rightBottom.Y; y++, newY++) { for (int x = leftTop.X, newX = 0; x <= rightBottom.X; x++, newX++) { media.Color color = this.GetPixelColor(x, y); newImageWrapper.SetPixelColor(newX, newY, color); } } return(newBitmap); }
//----------------------------------------------------------------------------------------------- public WriteableBitmap GetExtendedWriteableBitmapByOnePixelToBorder() { int pixelWidth = this.Image.PixelWidth; int pixelHeight = this.Image.PixelHeight; int stride = this.GetStride(); int dpiX = OS.OS.IntegerSystemDpiX; int dpiY = OS.OS.IntegerSystemDpiY; Int32Rect rect = new Int32Rect(0, 0, pixelWidth, pixelHeight); byte[] pixelsBytes = new byte[this.SizeInBytes]; this.Image.CopyPixels(rect, pixelsBytes, stride, 0); media.PixelFormat pixelFormat = this.Image.Format; WriteableBitmap newImage = WriteableBitmapCreator.CreateWriteableBitmap (pixelWidth + 2, pixelHeight + 2, dpiX, dpiY, pixelFormat); Int32Rect newRect = new Int32Rect(1, 1, pixelWidth, pixelHeight); newImage.WritePixels(newRect, pixelsBytes, stride, 0); return(newImage); }