C#處理JPEG頭資訊的方法

來源:互聯網
上載者:User

曾經有人給我了一張jpg,是1024*1024大小,檔案大小為99kb,做了個金字塔,各層影像大小如下:單位kb

我產生的jpg金字塔 為經過任何加工

height256 46.2kb

height512 162kb

height1024 557kb

Photoshop產生的jpg金字塔 為經過任何加工

height256 48kb

height512 90kb

height1024 163kb

可以看出這個映像大小差異太大了,我可是百思不得其解,最終看了上面那篇博文後,通過擷取中繼資料的方式來看看原因:

擷取中繼資料函數

複製代碼 代碼如下:public void GetProperty()

{

Bitmap myImage1024 = new Bitmap(@"E:\myjpg\height.jpg");

foreach (PropertyItem property in myImage1024.PropertyItems)

{

StringBuilder sb=new StringBuilder();

Byte[] sbbyte = (Byte[])property.Value;

sb.AppendFormat("ID:{0},Length:{1},Type:{2};\n",

property.Id.ToString(), property.Len.ToString(), property.Type.ToString(),);

Console.Write(sb);

}

}

發現我產生的jpg和原始圖片的中繼資料是不同的

height1024:

ID:771,Length:1,Type:1; 頭資訊Tag解釋:303 PropertyTagSRGBRenderingIntent

ID:769,Length:8,Type:5; 頭資訊Tag解釋:301 PropertyTagGamma

ID:20752,Length:1,Type:1; 頭資訊Tag解釋:5110 PropertyTagPixelUnit 解析度

ID:20753,Length:4,Type:4; 頭資訊Tag解釋:5111 PropertyTagPixelPerUnitX

ID:20754,Length:4,Type:4; 頭資訊Tag解釋:5112 PropertyTagPixelPerUnitY

height:

ID:20625,Length:128,Type:3;頭資訊Tag解釋:5091 PropertyTagChrominanceTable

ID:20624,Length:128,Type:3; 頭資訊Tag解釋:5090 PropertyTagLuminanceTable

原來如此,這樣我們就知道為什麼jpg竟然不一樣大小了,那好吧,我就修改了產生了影像金字塔的方法:在儲存金字塔之前,把他們的頭資訊去掉。之後驚奇的發現:我產生的影像金字塔的最精細層和原始映像一樣大小了!。

去除影像中繼資料的方法:

複製代碼 代碼如下:public void RemoveProperty()

{

Bitmap myImage1024 = new Bitmap(@"E:\myjpg\height.jpg");

foreach (PropertyItem property in myImage1024.PropertyItems)

{

myImage1024.RemovePropertyItem(property.Id);

}

myImage1024.Save(@"E:\myjpg\nopro.jpg");

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.