我试着用LockBits读取位图中的像素,但每次读取的时间大约是2-4秒。
这是一种方法:
public static Bitmap LockBits(Bitmap bmp)
{
PixelFormat pxf = PixelFormat.Format24bppRgb;
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData =
bmp.LockBits(rect, ImageLockMode.ReadWrite, pxf);
IntPtr ptr = bmpData.Scan0;
int numBytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[numBytes];
Marshal.Copy(ptr, rgbValues, 0, numBytes);
for (int counter = 0; counter < rgbValues.Length; counter += 6)
rgbValues[counter] = (byte)tolerancenumeric;
Marshal.Copy(rgbValues, 0, ptr, numBytes);
bmp.UnlockBits(bmpData);
bmp.Save(@"d:\testbmplockbits.bmp");
return bmp;
}这:(字节)容忍枚举在我更改它之前是值10,所以我可以从Form1数字向下更改这个值:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
CloudEnteringAlert.tolerancenum = (int)numericUpDown1.Value;
pictureBox1.Image = CloudEnteringAlert.LockBits(bitmapwithclouds);
}我原以为使用LockBits会使它更快,但是当我单击数字以改变它的值时,当程序运行时,它需要大约2-4秒的时间,直到值被更改,并且picturebox中的图像正在更新。
这个方法有什么问题?
发布于 2013-12-21 01:57:51
尝试刷新图片框:
pictureBox1.Refresh();发布于 2013-12-21 01:49:55
使用不安全,运行速度快。
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Bitmap test = new Bitmap(512, 512, PixelFormat.Format24bppRgb);
public Form1()
{
InitializeComponent();
numericUpDown1.Minimum = 0; numericUpDown1.Maximum = 255;
}
unsafe public static Bitmap LockBits(Bitmap bmp, int tolerancenumeric)
{
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
byte* ptr = (byte*)bmpData.Scan0;
int numBytes = bmpData.Stride * bmp.Height;
for (int counter = 0; counter < numBytes; counter += 6)
*(ptr + counter) = (byte)tolerancenumeric;
bmp.UnlockBits(bmpData);
return bmp;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
pictureBox1.Image = LockBits(test, (int)numericUpDown1.Value);
}
}
}https://stackoverflow.com/questions/20714485
复制相似问题