EDN Admin
Well-known member
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private Bitmap converttoBlackAndWhite(Bitmap original)
{
Bitmap newBitmap = <span style="color:Blue; new Bitmap(original.Width, original.Height);
<span style="color:Green; //get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
<span style="color:Green; //create the grayscale ColorMatrix
ColorMatrix colorMatrix = <span style="color:Blue; new ColorMatrix(
<span style="color:Blue; new <span style="color:Blue; float[][]
{
<span style="color:Blue; new <span style="color:Blue; float[] {.3f, .3f, .3f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {.59f, .59f, .59f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {.11f, .11f, .11f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {0, 0, 0, 1, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {0, 0, 0, 0, 1}
});
<span style="color:Green; //create some image attributes
ImageAttributes attributes = <span style="color:Blue; new ImageAttributes();
<span style="color:Green; //set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
<span style="color:Green; //draw the original image on the new image
<span style="color:Green; //using the grayscale color matrix
g.DrawImage(original, <span style="color:Blue; new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
<span style="color:Green; //dispose the Graphics object
g.Dispose();
<span style="color:Blue; return newBitmap;
}
[/code]
<br/>
The function converting the images to grayScale to gray(black and white).
Then after its working for some im getting error: Out of memory
Exception error:
System.OutOfMemoryException was unhandled by user code<br/>
Message=Out of memory.<br/>
Source=System.Drawing<br/>
StackTrace:<br/>
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)<br/>
at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData)<br/>
at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr)<br/>
at WmvAdapter.converttoBlackAndWhite(Bitmap original) in D:C-SharpExtracting_FramesExtracting_FramesExtracting_FramesWmvAdapter.cs:line 377<br/>
at WmvAdapter.DirectShowLib.ISampleGrabberCB.BufferCB(Double sampleTime, IntPtr pBuffer, Int32 bufferLen) in D:C-SharpExtracting_FramesExtracting_FramesExtracting_FramesWmvAdapter.cs:line 289<br/>
InnerException:
The error is on the line:
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),<br/>
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
The reason im using this finction since its working fast !
Thanks. <hr class="sig danieli
View the full article
<span style="color:Blue; private Bitmap converttoBlackAndWhite(Bitmap original)
{
Bitmap newBitmap = <span style="color:Blue; new Bitmap(original.Width, original.Height);
<span style="color:Green; //get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
<span style="color:Green; //create the grayscale ColorMatrix
ColorMatrix colorMatrix = <span style="color:Blue; new ColorMatrix(
<span style="color:Blue; new <span style="color:Blue; float[][]
{
<span style="color:Blue; new <span style="color:Blue; float[] {.3f, .3f, .3f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {.59f, .59f, .59f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {.11f, .11f, .11f, 0, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {0, 0, 0, 1, 0},
<span style="color:Blue; new <span style="color:Blue; float[] {0, 0, 0, 0, 1}
});
<span style="color:Green; //create some image attributes
ImageAttributes attributes = <span style="color:Blue; new ImageAttributes();
<span style="color:Green; //set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
<span style="color:Green; //draw the original image on the new image
<span style="color:Green; //using the grayscale color matrix
g.DrawImage(original, <span style="color:Blue; new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
<span style="color:Green; //dispose the Graphics object
g.Dispose();
<span style="color:Blue; return newBitmap;
}
[/code]
<br/>
The function converting the images to grayScale to gray(black and white).
Then after its working for some im getting error: Out of memory
Exception error:
System.OutOfMemoryException was unhandled by user code<br/>
Message=Out of memory.<br/>
Source=System.Drawing<br/>
StackTrace:<br/>
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)<br/>
at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData)<br/>
at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr)<br/>
at WmvAdapter.converttoBlackAndWhite(Bitmap original) in D:C-SharpExtracting_FramesExtracting_FramesExtracting_FramesWmvAdapter.cs:line 377<br/>
at WmvAdapter.DirectShowLib.ISampleGrabberCB.BufferCB(Double sampleTime, IntPtr pBuffer, Int32 bufferLen) in D:C-SharpExtracting_FramesExtracting_FramesExtracting_FramesWmvAdapter.cs:line 289<br/>
InnerException:
The error is on the line:
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),<br/>
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
The reason im using this finction since its working fast !
Thanks. <hr class="sig danieli
View the full article