本文整理汇总了C#中System.LockBits方法的典型用法代码示例。如果您正苦于以下问题:C# System.LockBits方法的具体用法?C# System.LockBits怎么用?C# System.LockBits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System
的用法示例。
在下文中一共展示了System.LockBits方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DirectXTexture
/// <summary>
/// Initializes a new DirectXTexture class.
/// </summary>
/// <param name="bmp">The Bitmap.</param>
internal DirectXTexture(System.Drawing.Bitmap bmp)
{
RawBitmap = (System.Drawing.Bitmap) bmp.Clone();
_width = bmp.Width;
_height = bmp.Height;
var sourceArea = new Rectangle(0, 0, bmp.Width, bmp.Height);
var bitmapProperties = new BitmapProperties(
new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), 96, 96);
var size = new Size2(bmp.Width, bmp.Height);
int stride = bmp.Width*sizeof (int);
using (var tempStream = new DataStream(bmp.Height*stride, true, true))
{
BitmapData bitmapData = bmp.LockBits(sourceArea, ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
for (int y = 0; y < bmp.Height; y++)
{
int offset = bitmapData.Stride*y;
for (int x = 0; x < bmp.Width; x++)
{
byte b = Marshal.ReadByte(bitmapData.Scan0, offset++);
byte g = Marshal.ReadByte(bitmapData.Scan0, offset++);
byte r = Marshal.ReadByte(bitmapData.Scan0, offset++);
byte a = Marshal.ReadByte(bitmapData.Scan0, offset++);
int rgba = r | (g << 8) | (b << 16) | (a << 24);
tempStream.Write(rgba);
}
}
bmp.UnlockBits(bitmapData);
tempStream.Position = 0;
_bmp = new Bitmap(DirectXHelper.RenderTarget, size, tempStream, stride, bitmapProperties);
}
}
示例2: TextureFromBitmapThreadSafe
public static Texture2D TextureFromBitmapThreadSafe(System.Drawing.Bitmap bmp)
{
int[] imgData = new int[bmp.Width * bmp.Height];
Texture2D texture;
texture = Texture2D.New(Engine.GraphicsContext.Device, bmp.Width, bmp.Height, PixelFormat.R8G8B8A8.UNorm);
unsafe
{
// lock bitmap
System.Drawing.Imaging.BitmapData origdata =
bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
uint* byteData = (uint*)origdata.Scan0;
// Switch bgra -> rgba
for (int i = 0; i < imgData.Length; i++)
{
byteData[i] = (byteData[i] & 0x000000ff) << 16 | (byteData[i] & 0x0000FF00) | (byteData[i] & 0x00FF0000) >> 16 | (byteData[i] & 0xFF000000);
}
// copy data
System.Runtime.InteropServices.Marshal.Copy(origdata.Scan0, imgData, 0, bmp.Width * bmp.Height);
byteData = null;
// unlock bitmap
bmp.UnlockBits(origdata);
}
// SetData uses the D3D11 Device Context which is not thread safe... execute
// the SetData inside a critical section using the GraphicsDevice as the mutex
Engine.GraphicsContext.ExclusiveDeviceExec(() => texture.SetData(imgData));
return texture;
}
示例3: TextureFromBitmap
public static Texture2D TextureFromBitmap(System.Drawing.Bitmap bmp)
{
int[] imgData = new int[bmp.Width * bmp.Height];
Texture2D texture;
texture = Texture2D.New(Engine.GraphicsContext.Device, bmp.Width, bmp.Height, PixelFormat.R8G8B8A8.UNorm);
unsafe
{
// lock bitmap
System.Drawing.Imaging.BitmapData origdata =
bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
uint* byteData = (uint*)origdata.Scan0;
// Switch bgra -> rgba
for (int i = 0; i < imgData.Length; i++)
{
byteData[i] = (byteData[i] & 0x000000ff) << 16 | (byteData[i] & 0x0000FF00) | (byteData[i] & 0x00FF0000) >> 16 | (byteData[i] & 0xFF000000);
}
// copy data
System.Runtime.InteropServices.Marshal.Copy(origdata.Scan0, imgData, 0, bmp.Width * bmp.Height);
byteData = null;
// unlock bitmap
bmp.UnlockBits(origdata);
}
texture.SetData(imgData);
return texture;
}
示例4: ToBitmap
public static void ToBitmap(this InteropBitmap img, System.Drawing.Bitmap bmp)
{
if (img == null) return;
int imgW = img.PixelWidth;
int imgH = img.PixelHeight;
byte[] byte_arr = new byte[(int)(4 * img.PixelWidth * img.PixelHeight)];
int stride = ((img.PixelWidth * img.Format.BitsPerPixel + 31) & ~31) >> 3;
img.CopyPixels(byte_arr, stride, 0);
System.Drawing.Imaging.BitmapData bData;
//The Width and Height should be static don't bother depending on the
//InteropBitmap for them
if (imgW == -1 || imgH == -1)
{
imgW = (int)img.PixelWidth;
imgH = (int)img.PixelHeight;
}
bData = bmp.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(), bmp.Size),
System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Marshal.Copy(byte_arr, 0, bData.Scan0, byte_arr.Length);
bmp.UnlockBits(bData);
}
示例5: SDXBitmapFromSysBitmap
private SharpDX.Direct2D1.Bitmap SDXBitmapFromSysBitmap(WindowRenderTarget device, System.Drawing.Bitmap bitmap)
{
var sourceArea = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
var bitmapProperties = new BitmapProperties(new PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied));
var size = new Size2(bitmap.Width, bitmap.Height);
// Transform pixels from BGRA to RGBA
int stride = bitmap.Width * sizeof(int);
using (var tempStream = new DataStream(bitmap.Height * stride, true, true))
{
// Lock System.Drawing.Bitmap
var bitmapData = bitmap.LockBits(sourceArea, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Convert all pixels
for (int y = 0; y < bitmap.Height; y++)
{
int offset = bitmapData.Stride * y;
for (int x = 0; x < bitmap.Width; x++)
{
// Not optimized
byte B = Marshal.ReadByte(bitmapData.Scan0, offset++);
byte G = Marshal.ReadByte(bitmapData.Scan0, offset++);
byte R = Marshal.ReadByte(bitmapData.Scan0, offset++);
byte A = Marshal.ReadByte(bitmapData.Scan0, offset++);
int rgba = R | (G << 8) | (B << 16) | (A << 24);
tempStream.Write(rgba);
}
}
bitmap.UnlockBits(bitmapData);
tempStream.Position = 0;
return new SharpDX.Direct2D1.Bitmap(device, size, tempStream, stride, bitmapProperties);
}
}
示例6: TextureFromBitmap
public static Texture2D TextureFromBitmap(Device device, System.Drawing.Bitmap bitmap)
{
BitmapData _BitmapData;
_BitmapData = bitmap.LockBits(
new DrawingRectangle(0, 0, bitmap.Width, bitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb
);
var _TextureDescription = new Texture2DDescription {
Width = bitmap.Width,
Height = bitmap.Height,
MipLevels = 1,
ArraySize = 1,
Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
Usage = ResourceUsage.Immutable,
BindFlags = BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None
};
_TextureDescription.SampleDescription.Count = 1;
_TextureDescription.SampleDescription.Quality = 0;
DataBox _Data;
_Data.DataPointer = _BitmapData.Scan0;
_Data.RowPitch = bitmap.Width * 4;
_Data.SlicePitch = 0;
var _Texture = new Texture2D(device, _TextureDescription, new[] { _Data });
bitmap.UnlockBits(_BitmapData);
return _Texture;
}
示例7: Initialise
public bool Initialise(System.Drawing.Bitmap bitmap)
{
RemoveAndDispose(ref _tex);
RemoveAndDispose(ref _texSRV);
//Debug.Assert(bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.Imaging.BitmapData bmData;
_texWidth = bitmap.Width;
_texHeight = bitmap.Height;
bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, _texWidth, _texHeight), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
try
{
Texture2DDescription texDesc = new Texture2DDescription();
texDesc.Width = _texWidth;
texDesc.Height = _texHeight;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm;
texDesc.SampleDescription.Count = 1;
texDesc.SampleDescription.Quality = 0;
texDesc.Usage = ResourceUsage.Immutable;
texDesc.BindFlags = BindFlags.ShaderResource;
texDesc.CpuAccessFlags = CpuAccessFlags.None;
texDesc.OptionFlags = ResourceOptionFlags.None;
SharpDX.DataBox data;
data.DataPointer = bmData.Scan0;
data.RowPitch = bmData.Stride;// _texWidth * 4;
data.SlicePitch = 0;
_tex = ToDispose(new Texture2D(_device, texDesc, new[] { data }));
if (_tex == null)
return false;
ShaderResourceViewDescription srvDesc = new ShaderResourceViewDescription();
srvDesc.Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm;
srvDesc.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D;
srvDesc.Texture2D.MipLevels = 1;
srvDesc.Texture2D.MostDetailedMip = 0;
_texSRV = ToDispose(new ShaderResourceView(_device, _tex, srvDesc));
if (_texSRV == null)
return false;
}
finally
{
bitmap.UnlockBits(bmData);
}
_initialised = true;
return true;
}
示例8: CopyPixelsFromDrawingBitmap
public static void CopyPixelsFromDrawingBitmap(System.Drawing.Bitmap source, int[] buffer)
{
System.Drawing.Imaging.BitmapData bitmapData = source.LockBits(
new System.Drawing.Rectangle(0, 0, source.Width, source.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb
);
System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, buffer, 0, source.Width * source.Height);
source.UnlockBits(bitmapData);
}
示例9: fromMemoryGDI
private TextureHandle fromMemoryGDI(System.Drawing.Bitmap bmp)
{
var locked = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var texRet = new SlimDX.Direct3D9.Texture(mDevice, bmp.Width, bmp.Height, 1, SlimDX.Direct3D9.Usage.None, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.Managed);
var surf = texRet.LockRectangle(0, SlimDX.Direct3D9.LockFlags.None);
surf.Data.WriteRange(locked.Scan0, locked.Stride * locked.Height);
texRet.UnlockRectangle(0);
bmp.UnlockBits(locked);
return new TextureHandle(texRet);
}
示例10: BitmapToByteArray
private static byte[] BitmapToByteArray(System.Drawing.Bitmap bitmap)
{
System.Drawing.Imaging.BitmapData bmpdata = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
int numbytes = bmpdata.Stride * bitmap.Height;
byte[] bytedata = new byte[numbytes];
IntPtr ptr = bmpdata.Scan0;
Marshal.Copy(ptr, bytedata, 0, numbytes);
bitmap.UnlockBits(bmpdata);
return bytedata;
}
示例11: LoadFromImage
/// <summary>
/// System.Drawing.ImageからSharpDX.Direct2D1.Bitmapを作成する
/// </summary>
/// <param name="renderTarget"></param>
/// <param name="bitmap"></param>
/// <returns></returns>
public static Bitmap LoadFromImage(RenderTarget renderTarget, string key, System.Drawing.Bitmap bitmap)
{
if (!ImageCash.ContainsKey(key))
{
var sourceArea = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
var imageint = new int[bitmap.Width * bitmap.Height];
// Lock System.Drawing.Bitmap
var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Convert all pixels
for (int y = 0; y < bitmap.Height; y++)
{
int offset = bitmapData.Stride * y;
for (int x = 0; x < bitmap.Width; x++)
{
// Not optimized
byte B = Marshal.ReadByte(bitmapData.Scan0, offset++);
byte G = Marshal.ReadByte(bitmapData.Scan0, offset++);
byte R = Marshal.ReadByte(bitmapData.Scan0, offset++);
byte A = Marshal.ReadByte(bitmapData.Scan0, offset++);
int rgba = R | (G << 8) | (B << 16) | (A << 24);
imageint[bitmap.Width * y + x] = rgba;
}
}
bitmap.UnlockBits(bitmapData);
ImageCash.Add(key, imageint);
}
// Transform pixels from BGRA to RGBA
int stride = bitmap.Width * sizeof(int);
using (var tempStream = new DataStream(bitmap.Height * stride, true, true))
{
var bitmapProperties =
new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied));
var size = new Size2(bitmap.Width, bitmap.Height);
var cash = ImageCash[key];
foreach (var rgba in cash) { tempStream.Write(rgba); }
tempStream.Position = 0;
return new Bitmap(renderTarget, size, tempStream, stride, bitmapProperties);
}
}
示例12: Wykonaj
public void Wykonaj(System.Drawing.Bitmap Bitmap, System.Collections.Generic.Stack<object> Argumenty)
{
Rectangle rect = new Rectangle(new Point(0,0), Bitmap.Size);
BitmapData bd = Bitmap.LockBits(rect, ImageLockMode.ReadWrite, Bitmap.PixelFormat);
int bytes = bd.Width * bd.Height * 3;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(bd.Scan0, rgbValues, 0, bytes);
for (int i = 0; i < bytes; i++)
{
rgbValues[i] = (byte)((int)rgbValues[i] & 0x7f);
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, bd.Scan0, bytes);
Bitmap.UnlockBits(bd);
}
示例13: FromSystemDrawingBitmap
/// <summary>
/// Creates a prefab bitmap given a Ststem.Drawing.Bitmap
/// </summary>
public static Bitmap FromSystemDrawingBitmap(System.Drawing.Bitmap inputBitmap){
int width = inputBitmap.Width;
int height = inputBitmap.Height;
int[] pixels = new int[width * height];
BitmapData bitmapData = inputBitmap.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb
);
System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, pixels, 0, pixels.Length);
inputBitmap.UnlockBits(bitmapData);
Bitmap toreturn = Bitmap.FromPixels (width, height, pixels);
return toreturn;
}
示例14: BitmapToIpImage
public IplImage BitmapToIpImage(System.Drawing.Bitmap bitmapImg)
{
/*
*
* 카메라로부터 받아온 비트맵 이미지를 OpenCV용 IplImage 화상으로 변환한다.
*
* */
IplImage retImage = Cv.CreateImage(new CvSize(bitmapImg.Width, bitmapImg.Height), BitDepth.U8, 3);
System.Drawing.Imaging.BitmapData bmpData = bitmapImg.LockBits(
new System.Drawing.Rectangle(0, 0, bitmapImg.Width, bitmapImg.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//변환한 비트맵 데이터를 IplImage 스트림으로 옮긴다.
CopyMemory(retImage.ImageData, bmpData.Scan0, bmpData.Stride * bmpData.Height);
bitmapImg.UnlockBits(bmpData);
return retImage;
}
示例15: DoAlgorithm
public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
{
//auteur: Jonathan Smit
unsafe
{
//afbeelding vastzetten in geheugen
BitmapData originalData = sourceImage.LockBits(
new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
//hoeveel bytes per pixel (voor jpg, 24-bit, is dat 24 gedeeld door 8 bit is 3 byte)
int pixelSize = 3;
//ipv 3x3 array gewoon een int-array van 3x3=9 waarden
int[] pixels = new int[9];
//voor n-keer 3x3: afbeelding hoogte en breedte - 2
for (int y = 0; y < sourceImage.Height - 2; y++)
{
//adres van de eerste pixel in elke rij van de afbeelding (scan0)
//adres van de eerste pixel in de rij
byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);
//voor het opslaan van de nieuwe gegevens beginnen we nog een rij lager
byte* nRow = (byte*)originalData.Scan0 + ((y + 1) * originalData.Stride);
int pixel;
for (int x = 0; x < sourceImage.Width - 2; x++)
{
//eerste rij
pixel = 0;
pixel = oRow[x * pixelSize];
pixel = (pixel << 8) | oRow[x * pixelSize + 1];
pixel = (pixel << 8) | oRow[x * pixelSize + 2];
pixels[0] = pixel;
pixel = 0;
pixel = oRow[x * pixelSize + 3];
pixel = (pixel << 8) | oRow[x * pixelSize + 4];
pixel = (pixel << 8) | oRow[x * pixelSize + 5];
pixels[1] = pixel;
pixel = 0;
pixel = oRow[x * pixelSize + 6];
pixel = (pixel << 8) | oRow[x * pixelSize + 7];
pixel = (pixel << 8) | oRow[x * pixelSize + 8];
pixels[2] = pixel;
//tweede rij
oRow = (byte*)originalData.Scan0 + ((y + 1) * originalData.Stride);
pixel = 0;
pixel = oRow[x * pixelSize];
pixel = (pixel << 8) | oRow[x * pixelSize + 1];
pixel = (pixel << 8) | oRow[x * pixelSize + 2];
pixels[3] = pixel;
pixel = 0;
pixel = oRow[x * pixelSize + 3];
pixel = (pixel << 8) | oRow[x * pixelSize + 4];
pixel = (pixel << 8) | oRow[x * pixelSize + 5];
pixels[4] = pixel;
pixel = 0;
pixel = oRow[x * pixelSize + 6];
pixel = (pixel << 8) | oRow[x * pixelSize + 7];
pixel = (pixel << 8) | oRow[x * pixelSize + 8];
pixels[5] = pixel;
//derde rij
oRow = (byte*)originalData.Scan0 + ((y + 2) * originalData.Stride);
pixel = 0;
pixel = oRow[x * pixelSize];
pixel = (pixel << 8) | oRow[x * pixelSize + 1];
pixel = (pixel << 8) | oRow[x * pixelSize + 2];
pixels[6] = pixel;
pixel = 0;
pixel = oRow[x * pixelSize + 3];
pixel = (pixel << 8) | oRow[x * pixelSize + 4];
pixel = (pixel << 8) | oRow[x * pixelSize + 5];
pixels[7] = pixel;
pixel = 0;
pixel = oRow[x * pixelSize + 6];
pixel = (pixel << 8) | oRow[x * pixelSize + 7];
pixel = (pixel << 8) | oRow[x * pixelSize + 8];
pixels[8] = pixel;
//sorteer
Array.Sort(pixels);
//nieuwe pixel
pixel = pixels[4];
//nieuwe pixel ontleden en weer invoegen
nRow[x * pixelSize + 3] = (byte)(pixel & 0xFF);
//.........这里部分代码省略.........