当前位置:编程学习 > C#/ASP.NET >>

怎样从bmp图片文件中获得信息(使用C#)

如果使用hbitmap便可以获得图片的信息,例如:img.GetPixel(i,j).R 。如果我现在要从bmp图片文件中获得R、G、B的颜色值,代码该如何写呢? --------------------编程问答-------------------- 请大家帮帮忙。。。只要能获得 一点的R、G、B的值就行,用什么方法都可以。。。 --------------------编程问答-------------------- 你最好不要将bmp文件转化成hbitmap在获得(虽然你已经知道怎样用hbitmap进行获值了),这样的话就叫做使用hbitmap了,就不是bmp的方法了 --------------------编程问答-------------------- 直接利用bmp的函数进行获取,应该也可以把 --------------------编程问答-------------------- 可是我该怎样用bmp图片文件获得颜色值呢?
我使用这条语句:FileStream bmpStream = File.OpenRead(@"d:/1.bmp"); 也不知道这样对不对,然后能使用bmpStream获得图片上一点的R、G、B的值了吗? --------------------编程问答-------------------- 大家快来帮帮忙啊。。。好像是一道很简单的问题。。。可是我不会。。。 --------------------编程问答-------------------- mark,关注 --------------------编程问答-------------------- 问题看起来的确好像就几条语句就能解决的,呵呵,等待明白的人来解答把。。。。。。。。 --------------------编程问答-------------------- public static bool Brightness(Bitmap b, int nBrightness) //增亮操作

if (nBrightness < -255 || nBrightness > 255) 
return false; 
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; 
} --------------------编程问答-------------------- 很感谢devilok() 同志,但是我的程序传的是string path,不是bitmap:
public CDGB GetImageFromBMPFile(string   path) --------------------编程问答-------------------- up --------------------编程问答-------------------- 大家来帮帮忙啊。。。从bmp文件中获得图像各点的颜色值。。。该函数传递的是 string   path(图像的路径)。。。 --------------------编程问答--------------------
public Color GetPixel (
int x,
int y
)
 


参数
x
要检索的像素的 x 坐标。 

y
要检索的像素的 y 坐标。 



返回值
Color 结构,它表示指定像素的颜色。 


public void GetPixel_Example(PaintEventArgs e)
{
             
    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");
             
    // Get the color of a pixel within myBitmap.
    Color pixelColor = myBitmap.GetPixel(50, 50);
             
    // Fill a rectangle with pixelColor.
    SolidBrush pixelBrush = new SolidBrush(pixelColor);
    e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
}

--------------------编程问答-------------------- 很感谢dilong_hcj(夏雨)同志,但是你这段是使用bitmap的,和我的要获得bmp文件的各点的颜色值也没有什么关系啊。。。我得方法是public CDGB GetImageFromBMPFile(string   path)
{。。。}   该方法是传递string path的,与bitmap无关。。。 --------------------编程问答-------------------- 学习!! ( 开源的基于ajax的可视化自定义web表单工具, 在: http://my5155.meibu.com ) --------------------编程问答-------------------- 日后再说~~~~~~~~~~~~~~~~~~~~~~~~~~

=============================================================
惊爆支持ASP、ASP.NET2.0空间500M+SQL数据库100M   特惠价格:128一年 
http://www.myidc.info/webhost/stylehost.aspx 


============================================================= --------------------编程问答-------------------- dilong_hcj(夏雨) 没错吧,你把Bitmap myBitmap = new Bitmap("Grapes.jpg")换成Bitmap myBitmap = new Bitmap("Grapes.bmp");不就可以获取bmp文件的各个点的颜色值了?
你那个public CDGB GetImageFromBMPFile中的CDGB  是什么东东?
--------------------编程问答-------------------- http://junjie.cnblogs.com/archive/2005/10/11/252156.html --------------------编程问答-------------------- mark --------------------编程问答-------------------- private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Bitmap img = new Bitmap(pictureBox1.Image);
            Color color = img.GetPixel(e.X, e.Y);
            byte r = color.R;
            byte g = color.G;
            byte b = color.B;
        } --------------------编程问答-------------------- private void SaveBitMap(Bitmap map,string sFile)
{
    StreamWriter writer = new StreamWriter(".\\" + sFile);
    int h = map.Size.Height;
    int w = map.Size.Width;
    for (int i = 0; i < h; i++)
    {
        for (int k = 0; k < w; k++)
        {
            Color co = map.GetPixel(i, k);
            writer.Write(string.Format("{0}:{1} RGB Value:R={2},G={3},B={4}", new object[] { i, k, co.R, co.G, co.B }));
        }
    }
    writer.Close();
} --------------------编程问答-------------------- 学习关注中... --------------------编程问答-------------------- mark --------------------编程问答-------------------- Bitmap类够全的 自己看MSDN --------------------编程问答-------------------- mark --------------------编程问答-------------------- 学习中~
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,