C#,ImageFormat怎么不一样呀???????????

来源:百度知道 编辑:UC知道 时间:2024/06/22 15:37:20
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.Drawing.Bitmap bp = new Bitmap(openFileDialog1.FileName);
pictureBox1.Image = bp;
textBox1.Text = bp.RawFormat.ToString();
textBox1.Text+=" "+System.Drawing.Imaging.ImageFormat.Jpeg.ToString();
}
}
显示:[ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e] Jpeg。
按理说应该显示为Jpeg Jpeg呀!
RawFormat的返回类型也是ImageFormat呀,而ImageFormat是Jpeg或者Gif之类的,怎么是一堆数字呢?

因为bp.RawFormat是System.Drawing.Imaging.ImageFormat类型的属性,也就是System.Drawing.Imaging.ImageFormat类型的一个对象。
MSDN查看System.Drawing.Imaging.ImageFormat类中的Jpeg,Gif之类都是静态属性,静态属性是属于类的,所以可以直接ImageFormat.Gif,而不是属于对象的,所以RawFormat中没有Gif这样的静态属性。
如果你要取得类型的字符串,可以直接取openFileDialog1.FileName字符串的小数点后面的字符串。
string extend=openFileDialog1.FileName.SubString(".");

RawFormat的toString代码我用reflector看了一下是这样的:
public override string ToString()
{
if (this == memoryBMP)
{
return "MemoryBMP";
}
if (this == bmp)
{
return "Bmp";
}
if (this == emf)
{
return "Emf";
}
if (this == wmf)
{
return "Wmf";
}
if (this == gif)
{
return "Gif";
}
if (this == jpeg)
{
return "Jpeg";
}
if (th