本文整理汇总了C#中System.Drawing.Bitmap.LockBits方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.LockBits方法的具体用法?C# Bitmap.LockBits怎么用?C# Bitmap.LockBits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Bitmap
的用法示例。
在下文中一共展示了Bitmap.LockBits方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddFrame
public void AddFrame(Bitmap bmp)
{
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
BitmapData bmpDat = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
if (countFrames == 0)
{
this.stride = (UInt32)bmpDat.Stride;
this.width = bmp.Width;
this.height = bmp.Height;
CreateStream();
}
int result = AviReadingMethods.AVIStreamWrite(aviStream,
countFrames, 1,
bmpDat.Scan0,
(Int32)(stride * height),
0, 0, 0);
if (result != 0)
{
throw new Exception("Problem podczas otwierania pliku AVI" + result.ToString());
}
bmp.UnlockBits(bmpDat);
countFrames++;
}
示例2: CalcDifference
public static void CalcDifference(Bitmap bmp1, Bitmap bmp2)
{
PixelFormat pxf = PixelFormat.Format32bppArgb;
Rectangle rect = new Rectangle(0, 0, bmp1.Width, bmp1.Height);
BitmapData bmpData1 = bmp1.LockBits(rect, ImageLockMode.ReadWrite, pxf);
IntPtr ptr1 = bmpData1.Scan0;
BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.ReadOnly, pxf);
IntPtr ptr2 = bmpData2.Scan0;
int numBytes = bmp1.Width * bmp1.Height * bytesPerPixel;
byte[] pixels1 = new byte[numBytes];
byte[] pixels2 = new byte[numBytes];
System.Runtime.InteropServices.Marshal.Copy(ptr1, pixels1, 0, numBytes);
System.Runtime.InteropServices.Marshal.Copy(ptr2, pixels2, 0, numBytes);
for (int i = 0; i < numBytes; i += bytesPerPixel)
{
if (pixels1[i + 0] == pixels2[i + 0] &&
pixels1[i + 1] == pixels2[i + 1] &&
pixels1[i + 2] == pixels2[i + 2])
{
pixels1[i + 0] = 255;
pixels1[i + 1] = 255;
pixels1[i + 2] = 255;
pixels1[i + 3] = 0;
}
}
System.Runtime.InteropServices.Marshal.Copy(pixels1, 0, ptr1, numBytes);
bmp1.UnlockBits(bmpData1);
bmp2.UnlockBits(bmpData2);
}
示例3: LoadTexture
public static int LoadTexture(string filename)
{
var bitmap = new Bitmap (filename);
int id = GL.GenTexture ();
BitmapData bmpData = bitmap.LockBits (
new Rectangle (0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.BindTexture (TextureTarget.Texture2D, id);
GL.TexImage2D (TextureTarget.Texture2D, 0,
PixelInternalFormat.Rgba,
bitmap.Width, bitmap.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
PixelType.UnsignedByte,
bmpData.Scan0);
bitmap.UnlockBits (bmpData);
GL.TexParameter (TextureTarget.Texture2D,
TextureParameterName.TextureMinFilter,
(int)TextureMinFilter.Linear);
GL.TexParameter (TextureTarget.Texture2D,
TextureParameterName.TextureMagFilter,
(int)TextureMagFilter.Linear);
return id;
}
示例4: CopyTextureToBitmap
public static Image CopyTextureToBitmap(D3D.Texture2D texture)
{
int width = texture.Description.Width;
if (width % 16 != 0)
width = MathExtensions.Round(width, 16) + 16;
Bitmap bmp = new Bitmap(texture.Description.Width, texture.Description.Height, PixelFormat.Format32bppArgb);
BitmapData bData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
using (DataStream stream = new DataStream(bData.Scan0, bData.Stride * bData.Height, false, true))
{
DataRectangle rect = texture.Map(0, D3D.MapMode.Read, D3D.MapFlags.None);
using (DataStream texStream = rect.Data)
{
for (int y = 0; y < texture.Description.Height; y++)
for (int x = 0; x < rect.Pitch; x+=4)
{
byte[] bytes = texStream.ReadRange<byte>(4);
if (x < bmp.Width*4)
{
stream.Write<byte>(bytes[2]); // DXGI format is BGRA, GDI format is RGBA.
stream.Write<byte>(bytes[1]);
stream.Write<byte>(bytes[0]);
stream.Write<byte>(255);
}
}
}
}
bmp.UnlockBits(bData);
return bmp;
}
示例5: ProcessBuffer
public int ProcessBuffer(double sampleTime, IntPtr buffer, int bufferLength)
{
using (Bitmap bitmap = new Bitmap(_width, _height, _format))
{
BitmapData data = bitmap.LockBits(_bounds, ImageLockMode.ReadWrite, _format);
NativeMethods.CopyMemory(data.Scan0, buffer, (uint) bufferLength);
bitmap.UnlockBits(data);
if (_flipImages) bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
UpdateImage(sampleTime, bitmap);
if (_flipImages) bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
data = bitmap.LockBits(_bounds, ImageLockMode.ReadOnly, _format);
NativeMethods.CopyMemory(buffer, data.Scan0, (uint) bufferLength);
bitmap.UnlockBits(data);
}
return 0;
}
示例6: DrawLines
private static PNM DrawLines(PNM image, IEnumerable<Tuple<Point, Point>> lines)
{
// prepare the bitmap
using (Bitmap bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb))
{
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte[] stridedBuffer = Stride(image.raster, image.Width, image.Height);
Marshal.Copy(stridedBuffer, 0, data.Scan0, stridedBuffer.Length);
bitmap.UnlockBits(data);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
// draw the lines
foreach(var tuple in lines)
{
graphics.DrawLine(Pens.Blue, tuple.Item1, tuple.Item2);
}
}
// get raw data
PNM lineImage = new PNM(image.Width, image.Height);
data = bitmap.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
lineImage.raster = UnStride(data.Scan0, image.Width, image.Height);
bitmap.UnlockBits(data);
return lineImage;
}
}
示例7: ToBitmap
public Bitmap ToBitmap(Int32 index, Bitmap bitmap)
{
BitmapData bdata;
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
switch (format)
{
case PixelFormat.PIXEL_FORMAT_RGB32:
bdata = bitmap.LockBits(rect, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
PXCMImage_ImageData_Copy(planes[index], pitches[index], bdata.Scan0, bdata.Stride, (Int32)bitmap.Width * 4, (Int32)bitmap.Height);
bitmap.UnlockBits(bdata);
break;
case PixelFormat.PIXEL_FORMAT_RGB24:
bdata = bitmap.LockBits(rect, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
PXCMImage_ImageData_Copy(planes[index], pitches[index], bdata.Scan0, bdata.Stride, (Int32)bitmap.Width * 3, (Int32)bitmap.Height);
bitmap.UnlockBits(bdata);
break;
case PixelFormat.PIXEL_FORMAT_DEPTH:
case PixelFormat.PIXEL_FORMAT_DEPTH_RAW:
bdata = bitmap.LockBits(rect, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
PXCMImage_ImageData_Copy(planes[index], pitches[index], bdata.Scan0, bdata.Stride, (Int32)bitmap.Width * 2, (Int32)bitmap.Height);
bitmap.UnlockBits(bdata);
break;
default:
return null;
}
return bitmap;
}
示例8: CreateTextTexture
/// <summary>
/// Create a texture of the given text in the given font.
/// </summary>
/// <param name="text">Text to display.</param>
/// <param name="font">Font to display the text as.</param>
/// <param name="size">Provides the size of the texture.</param>
/// <returns>Int32 Handle to the texture.</returns>
public static int CreateTextTexture(string text, Font font, out SizeF size)
{
int textureId;
size = GetStringSize(text, font);
using (Bitmap textBitmap = new Bitmap((int)size.Width, (int)size.Height))
{
GL.GenTextures(1, out textureId);
GL.BindTexture(TextureTarget.Texture2D, textureId);
BitmapData data =
textBitmap.LockBits(new Rectangle(0, 0, textBitmap.Width, textBitmap.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
(int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
(int)TextureMagFilter.Linear);
GL.Finish();
textBitmap.UnlockBits(data);
var gfx = System.Drawing.Graphics.FromImage(textBitmap);
gfx.Clear(Color.Transparent);
gfx.TextRenderingHint = TextRenderingHint.AntiAlias;
gfx.DrawString(text, font, new SolidBrush(Color.White), new RectangleF(0, 0, size.Width + 10, size.Height));
BitmapData data2 = textBitmap.LockBits(new Rectangle(0, 0, textBitmap.Width, textBitmap.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, textBitmap.Width, textBitmap.Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data2.Scan0);
textBitmap.UnlockBits(data2);
gfx.Dispose();
}
return textureId;
}
示例9: FromBitmap
public static Graphic FromBitmap(Bitmap bmp)
{
Graphic ret=null;
uint width=(uint)bmp.Width;
uint height=(uint)bmp.Height;
if ((bmp.PixelFormat&PixelFormat.Alpha)==PixelFormat.Alpha)
{
ret=new Graphic(width, height, Format.RGBA);
BitmapData data=bmp.LockBits(new Rectangle(0, 0, (int)width, (int)height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
Marshal.Copy(data.Scan0, ret.imageData, 0, (int)(width*height*4));
bmp.UnlockBits(data);
}
else
{
ret=new Graphic(width, height, Format.RGB);
BitmapData data=bmp.LockBits(new Rectangle(0, 0, (int)width, (int)height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
if (((int)width*3)==data.Stride)
{
Marshal.Copy(data.Scan0, ret.imageData, 0, (int)(width*height*3));
}
else
{
if (IntPtr.Size==4)
{
for (uint i=0; i<height; i++)
{
Marshal.Copy((IntPtr)(data.Scan0.ToInt32()+(int)(i*data.Stride)), ret.imageData, (int)(width*3*i), (int)(width*3));
}
}
else if (IntPtr.Size==8)
{
for (uint i=0; i<height; i++)
{
Marshal.Copy((IntPtr)(data.Scan0.ToInt64()+(long)(i*data.Stride)), ret.imageData, (int)(width*3*i), (int)(width*3));
}
}
}
bmp.UnlockBits(data);
data=null;
bmp.Dispose();
bmp=null;
}
return ret;
}
示例10: AddStaticResources
// Add the static resources defined in the map; if the map lives
// in a world use AddCustomTerrain instead
public static Bitmap AddStaticResources(Map map, Bitmap terrainBitmap)
{
Bitmap terrain = new Bitmap(terrainBitmap);
var tileset = Rules.TileSets[map.Tileset];
var bitmapData = terrain.LockBits(new Rectangle(0, 0, terrain.Width, terrain.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
int* c = (int*)bitmapData.Scan0;
for (var x = 0; x < map.Bounds.Width; x++)
for (var y = 0; y < map.Bounds.Height; y++)
{
var mapX = x + map.Bounds.Left;
var mapY = y + map.Bounds.Top;
if (map.MapResources.Value[mapX, mapY].type == 0)
continue;
var res = Rules.Info["world"].Traits.WithInterface<ResourceTypeInfo>()
.Where(t => t.ResourceType == map.MapResources.Value[mapX, mapY].type)
.Select(t => t.TerrainType).FirstOrDefault();
if (res == null)
continue;
*(c + (y * bitmapData.Stride >> 2) + x) = tileset.Terrain[res].Color.ToArgb();
}
}
terrain.UnlockBits(bitmapData);
return terrain;
}
示例11: ActorsBitmap
public static Bitmap ActorsBitmap(World world)
{
var map = world.Map;
var size = Util.NextPowerOf2(Math.Max(map.Bounds.Width, map.Bounds.Height));
Bitmap bitmap = new Bitmap(size, size);
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
int* c = (int*)bitmapData.Scan0;
foreach (var t in world.Queries.WithTrait<IRadarSignature>())
{
if (!world.LocalShroud.IsVisible(t.Actor))
continue;
var color = t.Trait.RadarSignatureColor(t.Actor);
foreach (var cell in t.Trait.RadarSignatureCells(t.Actor))
if (world.Map.IsInMap(cell))
*(c + ((cell.Y - world.Map.Bounds.Top) * bitmapData.Stride >> 2) + cell.X - world.Map.Bounds.Left) = color.ToArgb();
}
}
bitmap.UnlockBits(bitmapData);
return bitmap;
}
示例12: ReduceBitmap
public static Bitmap ReduceBitmap(Bitmap sourceImage)
{
if (sourceImage == null || sourceImage.Width <= 1 || sourceImage.Height <= 1)
return null;
Bitmap sourceImageBitmap = sourceImage as Bitmap;
if (sourceImageBitmap == null || sourceImageBitmap.PixelFormat != PixelFormat.Format1bppIndexed)
return null;
Bitmap tmpImage = new Bitmap(sourceImage.Width / 2, sourceImage.Height / 2, PixelFormat.Format1bppIndexed);
BitmapData sourceData = sourceImageBitmap.LockBits(new Rectangle(0, 0, sourceImageBitmap.Width, sourceImageBitmap.Height),
ImageLockMode.ReadOnly, sourceImageBitmap.PixelFormat);
BitmapData destData = tmpImage.LockBits(new Rectangle(0, 0, tmpImage.Width, tmpImage.Height),
ImageLockMode.ReadOnly, tmpImage.PixelFormat);
try
{
for (int i = 0; i < tmpImage.Height; i++)
{
#region Older
//byte* sourceRow = (byte*)sourceData.Scan0 + (i * 2 * sourceData.Stride);
//byte* destRow = (byte*)destData.Scan0 + (i * destData.Stride);
//for (int j = 0; j < destData.Stride; destRow[j++] = 0) ;
//for (int j = 0; j < tmpImage.Width; j++)
//{
// int sourceShift = 7 - (j % 4) * 2;
// destRow[j / 8] |= (byte)(((sourceRow[j / 4] & (1 << sourceShift)) >> sourceShift) << (7 - (j % 8)));
//}
#endregion
}
}
catch { }
sourceImageBitmap.UnlockBits(sourceData);
tmpImage.UnlockBits(destData);
return tmpImage;
}
示例13: Array1DToBitmapRGB
public static Bitmap Array1DToBitmapRGB(int[] input, int width, int height)
{
// create new grayscale image
Bitmap dstImg = new Bitmap(width, height);
// lock destination bitmap data
BitmapData dstData = dstImg.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int dstOffset = dstData.Stride - width * 3;
// do the job
unsafe
{
byte* dst = (byte*)dstData.Scan0.ToPointer();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++, dst += 3)
{
dst[RGB.R] = (byte)((input[y * width + x] & 0xFF0000) >> 16);
dst[RGB.G] = (byte)((input[y * width + x] & 0xFF00) >> 8);
dst[RGB.B] = (byte)((input[y * width + x] & 0xFF));
}
dst += dstOffset;
}
}
dstImg.UnlockBits(dstData);
return dstImg;
}
示例14: ConvertTo8bppFormat
public static Bitmap ConvertTo8bppFormat(Bitmap image)
{
// Create new image with 8BPP format
Bitmap destImage = new Bitmap(
image.Width,
image.Height,
PixelFormat.Format8bppIndexed
);
// Lock bitmap in memory
BitmapData bitmapData = destImage.LockBits(
new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadWrite,
destImage.PixelFormat
);
for (int i = 0; i < image.Width; i++)
{
for (int j = 0; j < image.Height; j++)
{
// Get source color
Color color = image.GetPixel(i, j);
// Get index of similar color
byte index = GetSimilarColor(destImage.Palette, color);
WriteBitmapData(i, j, index, bitmapData, 8);
}
}
destImage.UnlockBits(bitmapData);
return destImage;
}
示例15: AreEqual
public unsafe static bool AreEqual(Bitmap imageA, Bitmap imageB)
{
if (imageA.Width != imageB.Width) return false;
if (imageA.Height != imageB.Height) return false;
var d1 = imageA.LockBits(new Rectangle(0, 0, imageA.Width - 1, imageA.Height - 1), ImageLockMode.ReadOnly, imageA.PixelFormat);
var d2 = imageB.LockBits(new Rectangle(0, 0, imageB.Width - 1, imageB.Height - 1), ImageLockMode.ReadOnly, imageB.PixelFormat);
var data1 = (byte*)d1.Scan0;
var data2 = (byte*)d2.Scan0;
bool result = true;
for (int n = 0; n < d1.Height * d1.Stride; n++)
{
if (data1[n] != data2[n])
{
result = false;
break;
}
}
imageA.UnlockBits(d1);
imageB.UnlockBits(d2);
return result;
}