2010-03-04 221 views
11

我知道.NET Framework帶有一個圖像轉換類(System.Drawing.Image.Save方法)。將24位bmp轉換爲16位?

但我需要將一個24位(R8G8B8)位圖圖像轉換爲16位(X1R5G5B5),我真的不知道這種類型的轉換,以及一個24位到16位的變化BMP頭不起作用(因爲我們需要轉換整個圖像數據)。

此外,我想知道我是否能夠在圖像抖動控制等

想法? 任何形式的幫助,將不勝感激。

回答

18

格式16bppRgb1555像素格式被聲明,但GDI +實際上並不支持它。沒有使用該像素格式的主流視頻驅動程序或圖像編解碼器。 GDI +設計師猜到可能已經發生,他們的時間機器不夠準確。否則,從System.Drawing工作的程序員那裏得到一個相當潦草的複製/粘貼。

RGB555是可用的硬件和編解碼器最接近的匹配:

public static Bitmap ConvertTo16bpp(Image img) { 
    var bmp = new Bitmap(img.Width, img.Height, 
        System.Drawing.Imaging.PixelFormat.Format16bppRgb555); 
    using (var gr = Graphics.FromImage(bmp)) 
     gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height)); 
    return bmp; 
} 
1

您需要使用指定顏色深度的Encoder參數來保存位圖。

myEncoder = Encoder.ColorDepth; 
    myEncoderParameters = new EncoderParameters(1); 

    // Save the image with a color depth of 24 bits per pixel. 
    myEncoderParameter = new EncoderParameter(myEncoder, 24L); 
    myEncoderParameters.Param[0] = myEncoderParameter; 

    myBitmap.Save("MyBitmap.bmp", myImageCodecInfo, myEncoderParameters); 
+3

我測試了這一點,這是行不通的。不管是否通過24,8或32,輸出格式不會改變。我甚至可以傳遞像-8這樣的無效值。顏色深度從不改變。 – Elmue 2015-04-18 03:58:06

+0

取決於mimetype。得到它爲tif工作,沒有注意到不同格式的變化 – 2016-11-29 14:25:31

1

一個非常簡單的方式做,這是循環在舊的位圖數據和隱蔽的每對R8-G8-B8值來X1-R5-G5-B5,一個類似於此功能:

char condense(char i) 
{ 
    return (char)(i*255.0f/31.0f); 
} 

short transform(long input)// note that the last 8 bytes are ignored 
{ 
    return condense(input&0xff) || (condense((input&0xff00)>>8)<<5) 
    || (condense((intput&0xff0000)>>16)<<10); 
} 

// and somewhere in your program 
{ 
    int len; // the length of your data in pixels 
    char *data; // your data 
    char *newdata; // this is where you store the new data; make sure it's allocated 

    for(char *p=data; p<data+len*3; p+=3) 
    *(short *)newdata=transform(*(long *)data); 
}