Sprache: C#
Diese Funktion wandelt ein Bitmap in Graustufen um.
Allerdings schneller als http://dotnet-snippets.de/dns/bitmap-in-graustufen–wandeln-SID70.aspx
danke an herbivore
private Bitmap BildSW(Bitmap bmp)
{
int numBytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[numBytes];
int tmpSW;
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;
Marshal.Copy(ptr, rgbValues, 0, numBytes);
for(int counter = 0; counter < rgbValues.Length; counter+=3)
{
tmpSW = rgbValues[counter];
tmpSW += rgbValues[counter+1];
tmpSW += rgbValues[counter+2];
tmpSW /= 3;
rgbValues[counter] = rgbValues[counter+1] = rgbValues[counter+2] = Convert.ToByte(tmpSW);
}
Marshal.Copy(rgbValues, 0, ptr, numBytes);
bmp.UnlockBits(bmpData);
return bmp;
}
private Bitmap BildSW(Bitmap bmp)
{
int numBytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[numBytes];
int tmpSW;
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;
Marshal.Copy(ptr, rgbValues, 0, numBytes);
for(int counter = 0; counter < rgbValues.Length; counter+=3)
{
tmpSW = rgbValues[counter];
tmpSW += rgbValues[counter+1];
tmpSW += rgbValues[counter+2];
tmpSW /= 3;
rgbValues[counter] = rgbValues[counter+1] = rgbValues[counter+2] = Convert.ToByte(tmpSW);
}
Marshal.Copy(rgbValues, 0, ptr, numBytes);
bmp.UnlockBits(bmpData);
return bmp;
}
Alte URL:
/snippet/bitmap-schneller-in-graustufen-wandeln/1228
Das müsste noch schneller gehen mit der ColorMatrix von DotNet.
Fehler beim bmpData Zeil 1 in Methode
[code]public static Bitmap BildSW(Bitmap bmp)
{
int tmpSW;
PixelFormat pxf = PixelFormat.Format24bppRgb;
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, pxf);
int numBytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[numBytes];
IntPtr ptr = bmpData.Scan0;
Marshal.Copy(ptr, rgbValues, 0, numBytes);
for (int counter = 0; counter < rgbValues.Length; counter += 3) { tmpSW = rgbValues[counter]; tmpSW += rgbValues[counter + 1]; tmpSW += rgbValues[counter + 2]; tmpSW /= 3; rgbValues[counter] = rgbValues[counter + 1] = rgbValues[counter + 2] = Convert.ToByte(tmpSW); } Marshal.Copy(rgbValues, 0, ptr, numBytes); bmp.UnlockBits(bmpData); return bmp; }[/code]