本文整理汇总了C#中System.UnlockBits方法的典型用法代码示例。如果您正苦于以下问题:C# System.UnlockBits方法的具体用法?C# System.UnlockBits怎么用?C# System.UnlockBits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System
的用法示例。
在下文中一共展示了System.UnlockBits方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: 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;
}
示例3: 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);
}
示例4: 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);
}
}
示例5: 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);
}
示例6: 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);
}
示例7: 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;
}
示例8: 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);
}
示例9: 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;
}
示例10: 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;
}
示例11: AddFrame
// Add new frame to the AVI file
public void AddFrame(System.Drawing.Bitmap bmp)
{
// check image dimension
if ((bmp.Width != width) || (bmp.Height != height))
throw new ApplicationException("Invalid image dimension");
// lock bitmap data
BitmapData bmData = bmp.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// copy image data
int srcStride = bmData.Stride;
int dstStride = stride;
int src = bmData.Scan0.ToInt32() + srcStride * (height - 1);
int dst = buf.ToInt32();
for (int y = 0; y < height; y++)
{
Win32.memcpy(dst, src, dstStride);
dst += dstStride;
src -= srcStride;
}
// unlock bitmap data
bmp.UnlockBits(bmData);
// write to stream
if (Win32.AVIStreamWrite(streamCompressed, position, 1, buf,
stride * height, 0, IntPtr.Zero, IntPtr.Zero) != 0)
throw new ApplicationException("Failed adding frame");
position++;
}
示例12: 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);
}
}
示例13: updateImageDisplay
private void updateImageDisplay(System.Drawing.Bitmap bmp)
{
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new ThreadStart(delegate()
{
try
{
if (SimulatorImage.Source != null && bmp != null)
{
//Console.WriteLine("updating image");
WriteableBitmap bmpsrc = SimulatorImage.Source as WriteableBitmap;
if (bmpsrc != null && bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
{
//Console.WriteLine("copying");
int srcxoffset = (int)((bmp.Width - bmpsrc.Width) / 2);
int srcyoffset = (int)((bmp.Height - bmpsrc.Height) / 2);
int dstxoffset = 0;
int dstyoffset = 0;
if (srcxoffset < 0)
{
dstxoffset = -srcxoffset;
srcxoffset = 0;
}
if (srcyoffset < 0)
{
dstyoffset = -srcyoffset;
srcyoffset = 0;
}
int wid = Math.Min((int)bmpsrc.Width, bmp.Width + srcxoffset);
int hei = Math.Min((int)bmpsrc.Height, bmp.Height + srcyoffset);
var bmpdata = bmp.LockBits(
new System.Drawing.Rectangle(srcxoffset, srcyoffset, wid, hei),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
bmp.PixelFormat);
bmpsrc.WritePixels(new Int32Rect(dstxoffset, dstyoffset, wid, hei),
bmpdata.Scan0,
bmpdata.Stride * bmpdata.Height,
bmpdata.Stride);
bmp.UnlockBits(bmpdata);
bmp.Dispose();
}
}
}
catch (Exception)
{ }
}));
}
示例14: ToSharpDXBitmap
internal static Bitmap ToSharpDXBitmap(RenderTarget rt, System.Drawing.Bitmap image, float symbolScale)
{
if (image == null)
throw new ArgumentNullException("image");
if (image.PixelFormat != GdiPixelFormat.Format32bppPArgb)
return null;
var imageData = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
var dataStream = new DataStream(imageData.Scan0, imageData.Stride*imageData.Height, true, false);
var properties = new BitmapProperties
{
PixelFormat = new SharpDX.Direct2D1.PixelFormat
{
Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
AlphaMode = AlphaMode.Premultiplied
}
};
// ToDo apply scaling here!
//var scaler = new BitmapScaler(rt.Factory.NativePointer);
//scaler.
//Load the image from the gdi resource
var result = new Bitmap(rt, new Size2(image.Width, image.Height), dataStream, imageData.Stride, properties);
image.UnlockBits(imageData);
return result;
}
示例15: ApplyShadowToMask
/// <summary>
/// Apply shadow to mask onto the canvas
/// </summary>
/// <param name="clr">is the shadow color to be used</param>
/// <param name="mask">is the mask image to be read</param>
/// <param name="canvas">is the destination image to be draw upon</param>
/// <param name="maskColor">is mask color used in mask image</param>
/// <param name="offset">determine how much to offset the mask</param>
/// <returns>true if successful</returns>
public static bool ApplyShadowToMask(
System.Drawing.Color clrShadow,
System.Drawing.Bitmap mask,
System.Drawing.Bitmap canvas,
System.Drawing.Color maskColor,
System.Drawing.Point offset)
{
if (mask == null || canvas == null)
return false;
BitmapData bitmapDataMask = new BitmapData();
BitmapData bitmapDataCanvas = new BitmapData();
Rectangle rectCanvas = new Rectangle(0, 0, canvas.Width, canvas.Height);
Rectangle rectMask = new Rectangle(0, 0, mask.Width, mask.Height);
mask.LockBits(
rectMask,
ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb,
bitmapDataMask);
canvas.LockBits(
rectCanvas,
ImageLockMode.WriteOnly,
PixelFormat.Format32bppArgb,
bitmapDataCanvas);
unsafe
{
uint* pixelsMask = (uint*)bitmapDataMask.Scan0;
uint* pixelsCanvas = (uint*)bitmapDataCanvas.Scan0;
if (pixelsMask == null || pixelsCanvas == null)
return false;
uint col = 0;
int stride = bitmapDataCanvas.Stride >> 2;
for (uint row = 0; row < bitmapDataCanvas.Height; ++row)
{
for (col = 0; col < bitmapDataCanvas.Width; ++col)
{
if ((row - offset.Y) >= bitmapDataMask.Height || (col - offset.X) >= bitmapDataMask.Width ||
(row - offset.Y) < 0 || (col - offset.X) < 0)
continue;
uint index = (uint)(row * stride + col);
uint indexMask = (uint)((row - offset.Y) * (bitmapDataMask.Stride >> 2) + (col - offset.X));
byte maskByte = 0;
if (MaskColor.IsEqual(maskColor, MaskColor.Red))
maskByte = (Byte)((pixelsMask[indexMask] & 0xff0000) >> 16);
else if (MaskColor.IsEqual(maskColor, MaskColor.Green))
maskByte = (Byte)((pixelsMask[indexMask] & 0xff00) >> 8);
else if (MaskColor.IsEqual(maskColor, MaskColor.Blue))
maskByte = (Byte)(pixelsMask[indexMask] & 0xff);
uint color = (uint)(0xff << 24 | clrShadow.R << 16 | clrShadow.G << 8 | clrShadow.B);
if (maskByte > 0)
{
uint maskAlpha = (pixelsMask[indexMask] >> 24);
pixelsCanvas[index] = Alphablend(pixelsCanvas[index], color, (Byte)(maskAlpha), clrShadow.A);
}
}
}
}
canvas.UnlockBits(bitmapDataCanvas);
mask.UnlockBits(bitmapDataMask);
return true;
}