本文整理汇总了C#中System.Drawing.Bitmap.UnlockBits方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.UnlockBits方法的具体用法?C# Bitmap.UnlockBits怎么用?C# Bitmap.UnlockBits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Bitmap
的用法示例。
在下文中一共展示了Bitmap.UnlockBits方法的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: 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;
}
示例3: 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);
}
示例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: Parse
/// <summary>Loads a texture from the specified file.</summary>
/// <param name="file">The file that holds the texture.</param>
/// <param name="texture">Receives the texture.</param>
/// <returns>Whether loading the texture was successful.</returns>
internal bool Parse(string file, out Texture texture) {
/*
* Read the bitmap. This will be a bitmap of just
* any format, not necessarily the one that allows
* us to extract the bitmap data easily.
* */
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(file);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
/*
* If the bitmap format is not already 32-bit BGRA,
* then convert it to 32-bit BGRA.
* */
if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) {
Bitmap compatibleBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(compatibleBitmap);
graphics.DrawImage(bitmap, rect, rect, GraphicsUnit.Pixel);
graphics.Dispose();
bitmap.Dispose();
bitmap = compatibleBitmap;
}
/*
* Extract the raw bitmap data.
* */
BitmapData data = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);
if (data.Stride == 4 * data.Width) {
/*
* Copy the data from the bitmap
* to the array in BGRA format.
* */
byte[] raw = new byte[data.Stride * data.Height];
System.Runtime.InteropServices.Marshal.Copy(data.Scan0, raw, 0, data.Stride * data.Height);
bitmap.UnlockBits(data);
int width = bitmap.Width;
int height = bitmap.Height;
bitmap.Dispose();
/*
* Change the byte order from BGRA to RGBA.
* */
for (int i = 0; i < raw.Length; i += 4) {
byte temp = raw[i];
raw[i] = raw[i + 2];
raw[i + 2] = temp;
}
texture = new Texture(width, height, 32, raw);
return true;
} else {
/*
* The stride is invalid. This indicates that the
* CLI either does not implement the conversion to
* 32-bit BGRA correctly, or that the CLI has
* applied additional padding that we do not
* support.
* */
bitmap.UnlockBits(data);
bitmap.Dispose();
CurrentHost.ReportProblem(ProblemType.InvalidOperation, "Invalid stride encountered.");
texture = null;
return false;
}
}
示例7: 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;
}
}
示例8: 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;
}
示例9: 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;
}
示例10: 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;
}
示例11: ScreenShot
public static Bitmap ScreenShot(int xx, int yy, int width, int height, ReadBufferMode buffer)
{
Bitmap transfermap = new Bitmap(width, height);
System.Drawing.Imaging.BitmapData data =
transfermap.LockBits(new Rectangle(0, 0, transfermap.Width, transfermap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.ReadBuffer(buffer);
GL.ReadPixels(xx, yy, transfermap.Width, transfermap.Height, OpenTK.Graphics.OpenGL4.PixelFormat.Rgba, PixelType.UnsignedByte, data.Scan0);
unsafe
{
int PixelSize = 4;
unsafe
{
for (int y = 0; y < data.Height; y++)
{
byte* row = (byte*)data.Scan0 + (y * data.Stride);
for (int x = 0; x < data.Width; x++)
{
byte r = row[x * PixelSize + 2];
byte b = row[x * PixelSize];
row[x * PixelSize] = r;
row[x * PixelSize + 2] = b;
}
}
}
}
transfermap.UnlockBits(data);
transfermap.RotateFlip(RotateFlipType.RotateNoneFlipY);
return transfermap;
}
示例12: CalcDifference
public static void CalcDifference(Bitmap bmp1, Bitmap bmp2)
{
Rectangle rect = new Rectangle(0, 0, bmp1.Width, bmp1.Height);
BitmapData bitmapdata = bmp1.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
IntPtr source = bitmapdata.Scan0;
BitmapData data2 = bmp2.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
IntPtr ptr2 = data2.Scan0;
int length = (bmp1.Width * bmp1.Height) * 4;
byte[] destination = new byte[length];
byte[] buffer2 = new byte[length];
Marshal.Copy(source, destination, 0, length);
Marshal.Copy(ptr2, buffer2, 0, length);
for (int i = 0; i < length; i += 4)
{
if (((destination[i] == buffer2[i]) && (destination[i + 1] == buffer2[i + 1])) && (destination[i + 2] == buffer2[i + 2]))
{
destination[i] = 0xff;
destination[i + 1] = 0xff;
destination[i + 2] = 0xff;
destination[i + 3] = 0;
}
}
Marshal.Copy(destination, 0, source, length);
bmp1.UnlockBits(bitmapdata);
bmp2.UnlockBits(data2);
}
示例13: 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;
}
示例14: 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;
}
示例15: Texture
public Texture(string _mapPath, bool flipY = true)
{
using (Stream s = Interface.GetStreamFromPath (_mapPath)) {
try {
Map = _mapPath;
Bitmap bitmap = new Bitmap (s);
if (flipY)
bitmap.RotateFlip (RotateFlipType.RotateNoneFlipY);
BitmapData data = bitmap.LockBits (new System.Drawing.Rectangle (0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
createTexture (data.Scan0, data.Width, data.Height);
bitmap.UnlockBits (data);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
GL.TexParameter (TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.GenerateMipmap (GenerateMipmapTarget.Texture2D);
} catch (Exception ex) {
Debug.WriteLine ("Error loading texture: " + Map + ":" + ex.Message);
}
}
}