本文整理汇总了C#中Bitmap.LockBits方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.LockBits方法的具体用法?C# Bitmap.LockBits怎么用?C# Bitmap.LockBits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bitmap
的用法示例。
在下文中一共展示了Bitmap.LockBits方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LoadTexture
public static void LoadTexture()
{
Bitmap bitmap=null;
BitmapData bitmapData=null;
string[] tx={"relojes.bmp","ford1.bmp","benz1.jpg","motor.bmp","acelera.bmp","papel1.jpg","piso.jpg","madera2.jpg","madera3.jpg","papel1.jpg","Focus.jpg","fordrunner.jpg","mbne.jpg","particle.bmp","benz.jpg"};
Gl.glEnable(Gl.GL_TEXTURE_2D);
Gl.glEnable(Gl.GL_DEPTH_TEST);
// Gl.gl.Gl.glEnable(Gl.gl.Gl.gl_BLEND);
// Gl.gl.Gl.glBlendFunc(Gl.gl.Gl.gl_SRC_ALPHA,Gl.gl.Gl.gl_ONE_MINUS_SRC_ALPHA);
Rectangle rect;
texture=new int[tx.Length];
Gl.glGenTextures(tx.Length, texture);
for(int i=0; i<tx.Length; i++)
{
bitmap = new Bitmap(Application.StartupPath + "\\" + "Textures\\"+tx[i]);
rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
bitmapData =bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[i]);
Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, (int) Gl.GL_RGB8, bitmap.Width, bitmap.Height, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);
}
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_DECAL);
Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0);
bitmap.UnlockBits(bitmapData);
bitmap.Dispose();
}
示例2: Invert
public static bool Invert(Bitmap b)
{
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - b.Width * 3;
int nWidth = b.Width * 3;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < nWidth; ++x)
{
p[0] = (byte)(255 - p[0]);
++p;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
示例3: CopyRectangleToPoint
/// <summary>
/// Copies the rectangle to point.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="sourceRectangle">The source rectangle.</param>
/// <param name="destination">The destination.</param>
/// <param name="destinationPoint">The destination point.</param>
/// <param name="flip">if set to <c>true</c> [flip].</param>
public static void CopyRectangleToPoint( this Bitmap source, Rectangle sourceRectangle, Bitmap destination, Point destinationPoint, Palette palette, bool reverseX, bool reverseY )
{
BitmapData bmdSource = source.LockBits( new Rectangle( 0, 0, source.Width, source.Height ), ImageLockMode.ReadOnly, source.PixelFormat );
BitmapData bmdDest = destination.LockBits( new Rectangle( 0, 0, destination.Width, destination.Height ), ImageLockMode.WriteOnly, destination.PixelFormat );
int width = sourceRectangle.Width;
int height = sourceRectangle.Height;
int x = destinationPoint.X;
int y = destinationPoint.Y;
CalcOffset calcX = reverseX ?
(CalcOffset)(col => (width - col - 1)) :
(CalcOffset)(col => col);
CalcOffset calcY = reverseY ?
(CalcOffset)(row => (height - row - 1)) :
(CalcOffset)(row => row);
for (int col = 0; col < sourceRectangle.Width; col++)
{
for (int row = 0; row < sourceRectangle.Height; row++)
{
int index = bmdSource.GetPixel( col + sourceRectangle.X, row + sourceRectangle.Y );
if (palette.Colors[index % 16].A != 0)
{
bmdDest.SetPixel8bpp(
x + calcX( col ),
y + calcY( row ),
index );
}
}
}
source.UnlockBits( bmdSource );
destination.UnlockBits( bmdDest );
}
示例4: FindBlackRegionSize
public static void FindBlackRegionSize(Bitmap source, ref int leftH, ref int leftW, ref int rightH, ref int rightW)
{
int i = 0, j = 0;
int w = source.Width;
int h = source.Height;
BitmapData sourceData = source.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
leftH = leftW = int.MaxValue;
rightH = rightW = int.MinValue;
unsafe
{
byte* sourcePtr = (byte*)sourceData.Scan0;
for (i = 0; i < h; ++i)
{
for (j = 0; j < w; ++j)
{
if (sourcePtr[i * sourceData.Stride + j * 3] == 0)
{
if (i < leftH) { leftH = i; }
if (i > rightH) { rightH = i; }
if (j < leftW) { leftW = j; }
if (j > rightW) { rightW = j; }
}
}
}
source.UnlockBits(sourceData);
}
GC.Collect(0);
}
示例5: LoadTexture
static int LoadTexture(string filename)
{
if (String.IsNullOrEmpty(filename))
throw new ArgumentException(filename);
Bitmap bmp = new Bitmap(filename);
BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bmp.UnlockBits(bmp_data);
}
示例6: BitmapToByteArray
public static byte[] BitmapToByteArray(Bitmap bitmap)
{
BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), 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;
}
示例7: Process
public static Bitmap Process(Bitmap image, Rectangle region)
{
int startX = region.Left;
int startY = region.Top;
int stopX = image.Width - 1;
int stopY = image.Height - 1;
if (startX + region.Width < image.Width)
{
stopX = startX + region.Width;
}
if (startY + region.Height < image.Height)
{
stopY = startX + region.Height;
}
int numberOfPixels = (stopX - startX) * (stopY - startY);
Bitmap bitmap = new Bitmap(region.Width, region.Height);
Graphics g = Graphics.FromImage(bitmap);
g.DrawImage(image, new Rectangle(0, 0, region.Width, region.Height), region, GraphicsUnit.Pixel);
ImageStatistics rgbStatistics = new ImageStatistics(bitmap);
var b = new Bitmap(image);
BitmapData bmpData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, b.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int stride = bmpData.Stride;
int bytes = Math.Abs(bmpData.Stride) * b.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
Parallel.For(startY, stopY, y =>
{
for (int x = startX; x < stopX; x++)
{
int i = y * stride + x * 4;
//rgbValues[i] = curve[rgbValues[i]];
//rgbValues[i + 1] = curve[rgbValues[i + 1]];
//rgbValues[i + 2] = curve[rgbValues[i + 2]];
}
});
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
b.UnlockBits(bmpData);
return b;
}
示例8: Main
public static void Main(string[] args)
{
Console.WriteLine("");
if (args.Length < 2) usage();
mapObj map = new mapObj(args[0]);
Console.WriteLine("# Map layers " + map.numlayers + "; Map name = " + map.name);
for (int i = 0; i < map.numlayers; i++)
{
Console.WriteLine("Layer [" + i + "] name: " + map.getLayer(i).name);
}
try
{
Bitmap mapImage = new Bitmap(map.width, map.height, PixelFormat.Format32bppRgb);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (imageObj image = map.draw())
{
BitmapData bitmapData = mapImage.LockBits(new Rectangle(0, 0, image.width, image.height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
try
{
if (image.getRawPixels(bitmapData.Scan0) == (int)MS_RETURN_VALUE.MS_FAILURE)
{
Console.WriteLine("Unable to get image contents");
}
}
finally
{
mapImage.UnlockBits(bitmapData);
}
Console.WriteLine("Rendering time: " + stopwatch.ElapsedMilliseconds + "ms");
mapImage.Save(args[1]);
}
}
catch (Exception ex)
{
Console.WriteLine( "\nMessage ---\n{0}", ex.Message );
Console.WriteLine(
"\nHelpLink ---\n{0}", ex.HelpLink );
Console.WriteLine( "\nSource ---\n{0}", ex.Source );
Console.WriteLine(
"\nStackTrace ---\n{0}", ex.StackTrace );
Console.WriteLine(
"\nTargetSite ---\n{0}", ex.TargetSite ); }
}
示例9: create_image
Image create_image()
{
Stopwatch stopWatch = Stopwatch.StartNew();
do_mandel();
do_map_to_argb();
stopWatch.Stop();
m_elapsed = String.Format("{0} milliseconds", stopWatch.Elapsed.TotalMilliseconds);
Bitmap bmp = new Bitmap(N, N);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat);
Marshal.Copy(argb_array, 0, bmpdata.Scan0, argb_array.Length);
bmp.UnlockBits(bmpdata);
Graphics g = Graphics.FromImage(bmp);
g.DrawString(m_elapsed, SystemFonts.MessageBoxFont, new SolidBrush(Color.White), new PointF(20, 20));
return bmp;
}
示例10: Color
public static bool Color(Bitmap b, int red, int green, int blue)
{
if (red < -255 || red > 255) return false;
if (green < -255 || green > 255) return false;
if (blue < -255 || blue > 255) return false;
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - b.Width * 3;
int nPixel;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < b.Width; ++x)
{
nPixel = p[2] + red;
nPixel = Math.Max(nPixel, 0);
p[2] = (byte)Math.Min(255, nPixel);
nPixel = p[1] + green;
nPixel = Math.Max(nPixel, 0);
p[1] = (byte)Math.Min(255, nPixel);
nPixel = p[0] + blue;
nPixel = Math.Max(nPixel, 0);
p[0] = (byte)Math.Min(255, nPixel);
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
示例11: Count
static unsafe Tuple<double, double, double> Count(string file)
{
var bmp = new Bitmap(file);
var rets = new Tuple<double, double, double>[4];
double x = 0.0, y = 0.0, z = 0.0;
BitmapData data = bmp.LockBits(new Rectangle(new Point(0, 0), bmp.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
byte* p = (byte*)data.Scan0.ToPointer();
int unit_height = data.Height / 4;
int unit = unit_height * data.Stride;
Parallel.Invoke(
() => CountCore(ref rets[0], p, data.Width, unit_height, data.Stride),
() => CountCore(ref rets[1], p + unit, data.Width, unit_height, data.Stride),
() => CountCore(ref rets[2], p + 2 * unit, data.Width, unit_height, data.Stride),
() => CountCore(ref rets[3], p + 3 * unit, data.Width, data.Height - 3 * unit_height, data.Stride)
);
bmp.UnlockBits(data);
foreach (var item in rets)
{
x += item.Item1;
y += item.Item2;
z += item.Item3;
}
/*
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
Color c = bmp.GetPixel(i, j);
x += (double)c.R;
y += (double)c.G;
z += (double)c.B;
}
}
*/
Console.WriteLine(((int)x).ToString() + "," + ((int)y).ToString() + "," + ((int)z).ToString());
double area = bmp.Width * bmp.Height;
return new Tuple<double, double, double>(x/area, y/area, z/area);
}
示例12: Load_Texture
protected int Load_Texture(string filename, int xx, int yy)
{
int texture_id;
Bitmap bitmap = new Bitmap(filename);
bitmap.MakeTransparent(Color.Magenta);
int px_x = (xx * TILE_W);
int px_y = (yy * TILE_H);
GL.GenTextures(1, out texture_id);
GL.BindTexture(TextureTarget.Texture2D, texture_id);
Check_for_GL_error("After calling GL.BindTexture()");
Rectangle rect = new Rectangle(px_x, px_y, TILE_W, TILE_H);
BitmapData data = bitmap.LockBits(rect,
ImageLockMode.ReadOnly,
//System.Drawing.Imaging.PixelFormat.Format32bppPArgb); // With this line uncommented, it works on XP and Ubuntu
//System.Drawing.Imaging.PixelFormat.Format32bppRgb); // This value worked on Windows 7, but on XP I see black pixels rather than transparent
System.Drawing.Imaging.PixelFormat.Format32bppArgb); // With this line uncommented, I get "scrambled" results on XP
// Format32bppArgb was indicated by OpenTK.com user "Inertia", citing http://msdn.microsoft.com/en-us/library/8517ckds.aspx
// However, rather than transparency, I observe a strange display (sending a screen shot)
GL.TexImage2D(
OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, // texture_target,
0, // level,
OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgba, // internal_format
data.Width, data.Height, // width, height,
0, // border,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, // pixel_format
OpenTK.Graphics.OpenGL.PixelType.UnsignedByte, // pixel_type
data.Scan0 // pixels
);
Check_for_GL_error("After calling GL.TexImage2D()");
bitmap.UnlockBits(data);
bitmap.Dispose();
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Linear);
return texture_id;
}
示例13: Brightness
public static bool Brightness(Bitmap b, int nBrightness)
{
if (nBrightness < -255 || nBrightness > 255)
return false;
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
int nVal = 0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - b.Width * 3;
int nWidth = b.Width * 3;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < nWidth; ++x)
{
nVal = (int)(p[0] + nBrightness);
if (nVal < 0) nVal = 0;
if (nVal > 255) nVal = 255;
p[0] = (byte)nVal;
++p;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
示例14: Open
public Bitmap Open(string fileName, UInt32 frameRate, int width, int height)
{
frameRate_ = frameRate;
width_ = (UInt32)width;
height_ = (UInt32)height;
bmp_ = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData bmpDat = bmp_.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
stride_ = (UInt32)bmpDat.Stride;
bmp_.UnlockBits(bmpDat);
AVIFileInit();
int hr = AVIFileOpenW(ref pfile_, fileName, 4097 /* OF_WRITE | OF_CREATE (winbase.h) */, 0);
if (hr != 0)
{
throw new AviException("error for AVIFileOpenW");
}
CreateStream();
SetOptions();
return bmp_;
}
示例15: AddFrame
public void AddFrame(Bitmap new_bmp)
{
BitmapData bmpDat = new_bmp.LockBits(
new Rectangle(0, 0, (int)width_, (int)height_), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
int hr = AVIStreamWrite(psCompressed_, count_, 1,
bmpDat.Scan0, // pointer to data
(Int32)(stride_ * height_),
0, // 16 = AVIIF_KEYFRAMe
0,
0);
if (hr != 0)
{
throw new AviException("AVIStreamWrite");
}
new_bmp.UnlockBits(bmpDat);
count_++;
}