问题描述
C#图像处理我先打开一幅彩色图像到picturebox中,然后我想用内存法将这幅彩色图像(已经在picturebox)转换为灰度图像,并显示在同一个picturebox中请问这怎么实现?(现在我可以进行打开一幅图片,将图片显示到picturebox中,以及我明白内存法的原理,但就是不能将图片用内存法转换为灰度图像并显示在picturebox中)
解决方案
解决方案二:
什么是内存法?
解决方案三:
图像处理中我看资料的时候,上面提及C#图像处理的3种方法,一是提取像素法二是内存法三是指针法,这三种方法处理图像的速度不同,提取像素法速度最慢,指针法最快。我现在可以用提取像素法做到图像在picturebox中的处理,但是用内存法不行
解决方案四:
引用1楼xuzuning的回复:
什么是内存法?
图像处理中我看资料的时候,上面提及C#图像处理的3种方法,一是提取像素法二是内存法三是指针法,这三种方法处理图像的速度不同,提取像素法速度最慢,指针法最快。我现在可以用提取像素法做到图像在picturebox中的处理,但是用内存法不行
解决方案五:
引用1楼xuzuning的回复:
什么是内存法?
我自己解决啦谢谢你~~!!
解决方案六:
使用指针最快,也很方便
解决方案七:
publicstaticBitmapGetDarkImage(BitmapbmpSrc){Bitmapbmp=bmpSrc.Clone(newRectangle(0,0,bmpSrc.Width,bmpSrc.Height),PixelFormat.Format24bppRgb//以24位图像作为处理);BitmapDatabmpData=bmp.LockBits(//锁定位图newRectangle(0,0,bmp.Width,bmp.Height),ImageLockMode.ReadWrite,bmp.PixelFormat);//计算大小注意后面不是width因为会内存中系统会补齐byte[]byColorInfo=newbyte[bmp.Height*bmpData.Stride];//将rgb数据拷贝到数组中去Marshal.Copy(bmpData.Scan0,byColorInfo,0,byColorInfo.Length);for(intx=0,xLen=bmp.Width;x<xLen;x++){for(inty=0,yLen=bmp.Height;y<yLen;y++){bytebyV=GetAvg(//自己写的一个计算平均值的方法byColorInfo[y*bmpData.Stride+x*3],//BbyColorInfo[y*bmpData.Stride+x*3+1],//GbyColorInfo[y*bmpData.Stride+x*3+2]);//RbyColorInfo[y*bmpData.Stride+x*3]=byColorInfo[y*bmpData.Stride+x*3+1]=byColorInfo[y*bmpData.Stride+x*3+2]=byV;}}Marshal.Copy(byColorInfo,0,bmpData.Scan0,byColorInfo.Length);bmp.UnlockBits(bmpData);//操作完拷贝回去解锁returnbmp;}
以前的一个黑白处理的代码写上了注释你自己参考吧
解决方案八:
Bitmapbmp=newBitmap(100,100);using(Graphicsg=Graphics.FromImage(bmp)){g.FillEllipse(Brushes.Red,newRectangle(0,0,80,80));g.FillRectangle(newSolidBrush(Color.FromArgb(128,Color.Green)),newRectangle(20,20,80,80));}e.Graphics.DrawImage(bmp,newPoint(10,10));//简单的ControlPaint.DrawImageDisabled(e.Graphics,bmp,120,10,Color.White);//用颜色矩阵System.Drawing.Imaging.ImageAttributesattr=newSystem.Drawing.Imaging.ImageAttributes();float[][]colorMatrixElements={newfloat[]{.33f,.33f,.33f,0,0},//r=(r+g+b)/3newfloat[]{.33f,.33f,.33f,0,0},//g=(r+g+b)/3newfloat[]{.33f,.33f,.33f,0,0},//b=(r+g+b)/3newfloat[]{0,0,0,1,0},//alphascalingfactorof1newfloat[]{0,0,0,0,1}};//System.Drawing.Imaging.ColorMatrixmatrix=newSystem.Drawing.Imaging.ColorMatrix(colorMatrixElements);attr.SetColorMatrix(matrix);e.Graphics.DrawImage(bmp,newRectangle(10,120,100,100),0,0,100,100,GraphicsUnit.Pixel,attr);