private void GetBitmapRectangle() { // Define format, manipulation mode and area to modify System.Drawing.Imaging.PixelFormat pxf = System.Drawing.Imaging.PixelFormat.Format24bppRgb; System.Drawing.Imaging.ImageLockMode ilm = System.Drawing.Imaging.ImageLockMode.ReadWrite; Rectangle r = new Rectangle(0, 0, bm.Width, bm.Height); // Lock the bitmap bits System.Drawing.Imaging.BitmapData bmData = this.bm.LockBits(r, ilm, pxf); // Get the address of the first line IntPtr ptr = bmData.Scan0; // Declare an array to hold the bytes of the bitmap int numBytes = bm.Width * bm.Height * 3; byte[] rgbValues = new byte[numBytes]; // Copy the RGB values into the array. System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, numBytes); // Manipulate the bitmap, such as changing the // blue value for every other pixel in the bitmap. for (int counter = 0; counter < rgbValues.Length; counter += 6) { rgbValues[counter] = 200; } // Copy the RGB values back to the bitmap System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, numBytes); // Unlock the bits bm.UnlockBits(bmData); }
void FilerImage() { // Copy Image to Bitmap System.Drawing.Rectangle ImageBounds = new System.Drawing.Rectangle(0, 0, Bitmap.Width, Bitmap.Height); System.Drawing.Imaging.ImageLockMode Mode = System.Drawing.Imaging.ImageLockMode.ReadWrite; System.Drawing.Imaging.PixelFormat Format = Bitmap.PixelFormat; System.Drawing.Imaging.BitmapData BitmapData = Bitmap.LockBits(ImageBounds, Mode, Format); System.IntPtr ptr = BitmapData.Scan0; System.Runtime.InteropServices.Marshal.Copy(FilterdAndCroppedDepthImage, 0, ptr, FilterdAndCroppedDepthImage.Length); Bitmap.UnlockBits(BitmapData); //Apply Filter Blur.ApplyInPlace(Bitmap); //Copy Bitmap back to Image BitmapData = Bitmap.LockBits(ImageBounds, Mode, Format); ptr = BitmapData.Scan0; System.Runtime.InteropServices.Marshal.Copy(ptr, FilterdAndCroppedDepthImage, 0, FilterdAndCroppedDepthImage.Length); Bitmap.UnlockBits(BitmapData); }
void FilerImage() { // Copy Image to Bitmap System.Drawing.Rectangle ImageBounds = new System.Drawing.Rectangle(0, 0, Bitmap.Width, Bitmap.Height); System.Drawing.Imaging.ImageLockMode Mode = System.Drawing.Imaging.ImageLockMode.ReadWrite; System.Drawing.Imaging.PixelFormat Format = Bitmap.PixelFormat; System.Drawing.Imaging.BitmapData BitmapData = Bitmap.LockBits(ImageBounds, Mode, Format); System.IntPtr ptr = BitmapData.Scan0; short[] shortBuffer = Array.ConvertAll(FilterdAndCroppedDepthImage, (value) => (short)value); System.Runtime.InteropServices.Marshal.Copy(shortBuffer, 0, ptr, shortBuffer.Length); Bitmap.UnlockBits(BitmapData); //Apply Filter //Blur.ApplyInPlace(Bitmap); //Copy Bitmap back to Image BitmapData = Bitmap.LockBits(ImageBounds, Mode, Format); ptr = BitmapData.Scan0; System.Runtime.InteropServices.Marshal.Copy(ptr, shortBuffer, 0, shortBuffer.Length); FilterdAndCroppedDepthImage = Array.ConvertAll(shortBuffer, (value) => (ushort)value); Bitmap.UnlockBits(BitmapData); }
public void OpenData(System.Drawing.Imaging.ImageLockMode mode, bool flip) { this.data = this.bitmap.LockBits(new System.Drawing.Rectangle(0, 0, this.width, this.height), mode, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); unsafe { this.pixels = (byte *)this.data.Scan0; if (flip) { this.pixels += this.height * this.data.Stride; } } }
// 这是用LockBits方式计算的,速度较快。 /// <summary> /// 根据色板获取位图。 /// </summary> /// <param name="colorPalette"></param> /// <returns></returns> private static Bitmap CreateTextureImage(this ColorPalette colorPalette, int width = 1000, int height = 1) { System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Format32bppRgb; System.Drawing.Imaging.ImageLockMode lockMode = System.Drawing.Imaging.ImageLockMode.WriteOnly; System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, format); Rectangle bitmapRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(bitmapRect, lockMode, format); int length = Math.Abs(bmpData.Stride) * bitmap.Height; byte[] bitmapBytes = new byte[length]; for (int i = 0; i < colorPalette.Colors.Length - 1; i++) { int left = (int)(width * colorPalette.Coords[i]); int right = (int)(width * colorPalette.Coords[i + 1]); GLColor leftColor = colorPalette.Colors[i]; GLColor rightColor = colorPalette.Colors[i + 1]; for (int col = left; col < right; col++) { Color color = (leftColor * ((right - col) * 1.0f / (right - left)) + rightColor * ((col - left) * 1.0f / (right - left))); for (int row = 0; row < height; row++) { bitmapBytes[row * bmpData.Stride + col * 4 + 0] = color.B; bitmapBytes[row * bmpData.Stride + col * 4 + 1] = color.G; bitmapBytes[row * bmpData.Stride + col * 4 + 2] = color.R; } } } System.Runtime.InteropServices.Marshal.Copy(bitmapBytes, 0, bmpData.Scan0, length); bitmap.UnlockBits(bmpData); return(bitmap); }
/// <summary> /// 根据<paramref name="charInfoDict"/>等信息把各个字形写入一个大的位图并返回之。 /// </summary> /// <param name="face"></param> /// <param name="fontHeight"></param> /// <param name="firstChar"></param> /// <param name="lastChar"></param> /// <param name="maxTextureWidth"></param> /// <param name="charInfoDict"></param> /// <param name="textureWidth"></param> /// <param name="textureHeight"></param> /// <returns></returns> private static IEnumerable <TTFTextureYeildingState> GetBigBitmap( FreeTypeFace face, int fontHeight, int maxTextureWidth, char firstChar, char lastChar, Dictionary <char, CharacterInfo> charInfoDict, int textureWidth, int textureHeight) { int count = lastChar - firstChar; int index = 0; System.Drawing.Bitmap bigBitmap = new System.Drawing.Bitmap(textureWidth, textureHeight); Graphics graphics = Graphics.FromImage(bigBitmap); for (char c = firstChar; c <= lastChar; c++) { FreeTypeBitmapGlyph glyph = new FreeTypeBitmapGlyph(face, c, fontHeight); int size = glyph.obj.bitmap.width * glyph.obj.bitmap.rows; { bool zeroBuffer = glyph.obj.bitmap.buffer == IntPtr.Zero; if ((size == 0) && (!zeroBuffer)) { throw new Exception(string.Format("glyph size({0}) for non zero buffer({1})", 0, glyph.obj.bitmap.buffer)); } if ((size != 0) && zeroBuffer) { throw new Exception(string.Format("glyph size({0}) for zero buffer({1})", size, glyph.obj.bitmap.buffer)); } } byte[] byteBitmap = null; if ((c == ' ') && (size == 0)) { size = charInfoDict[' '].width * charInfoDict[' '].height; byteBitmap = new byte[size]; } else if ((c == '\t') && (size == 0)) { size = charInfoDict['\t'].width * charInfoDict['\t'].height; byteBitmap = new byte[size]; } else if (size != 0) { byteBitmap = new byte[size]; Marshal.Copy(glyph.obj.bitmap.buffer, byteBitmap, 0, byteBitmap.Length); } if (size != 0) { CharacterInfo cInfo; if (charInfoDict.TryGetValue(c, out cInfo)) { if (cInfo.width > 0 && cInfo.height > 0) { System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Format32bppRgb; System.Drawing.Imaging.ImageLockMode lockMode = System.Drawing.Imaging.ImageLockMode.WriteOnly; System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(cInfo.width, cInfo.height, format); Rectangle bitmapRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(bitmapRect, lockMode, format); int length = Math.Abs(bmpData.Stride) * bitmap.Height; byte[] bitmapBytes = new byte[length]; for (int row = 0; row < cInfo.height; row++) { for (int col = 0; col < cInfo.width; col++) { byte color = byteBitmap[row * cInfo.width + col]; bitmapBytes[row * bmpData.Stride + col * 4 + 0] = color; bitmapBytes[row * bmpData.Stride + col * 4 + 1] = color; bitmapBytes[row * bmpData.Stride + col * 4 + 2] = color; //bitmapBytes[row * bmpData.Stride + col * 4 + 3] = color; } } System.Runtime.InteropServices.Marshal.Copy(bitmapBytes, 0, bmpData.Scan0, length); bitmap.UnlockBits(bmpData); //int baseLine = fontHeight * 3 / 4 + 4; //graphics.DrawImage(bitmap, cInfo.xoffset, cInfo.yoffset + baseLine - glyph.obj.top); int skyHeight = fontHeight * 3 / 4 - glyph.obj.top; if (skyHeight < 0) { skyHeight = 0; } graphics.DrawImage(bitmap, cInfo.xoffset, cInfo.yoffset + skyHeight); } } else { throw new Exception(string.Format("Not support for display the char [{0}]", c)); } } if (c == char.MaxValue) { break; } yield return(new TTFTextureYeildingState() { percent = index++ *100 / count, message = c.ToString(),//string.Format("print glyph for [{0}]", c), }); } graphics.Dispose(); yield return(new TTFTextureYeildingState() { percent = index++ *100 / count, bigBitmap = bigBitmap, message = "texture is ready", }); }
public System.Drawing.Imaging.BitmapData LockBits(Rectangle rect, System.Drawing.Imaging.ImageLockMode flags, System.Drawing.Imaging.PixelFormat format, System.Drawing.Imaging.BitmapData bitmapData) { }
public BitmapWithAccess(Bitmap bitmap, System.Drawing.Imaging.ImageLockMode lockMode) { Bitmap = bitmap; BitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), lockMode, System.Drawing.Imaging.PixelFormat.Format32bppArgb); }