![](/img/trans.png)
[英]How to place a Rectangle in a new image with different size, while preserving the size and quality of the orignial Rectangle?
[英]Preserving Image quality
我有一個winform C#桌面應用程序。
我有一個恆定的jpeg流進來。我正在比較當前的圖像與之前的1.通過使用第三方工具 - Emgu - 我可以創建一個只包含差異的新圖像。
然后我將該圖像轉換為內存流,然后轉換為字節數組。
在接收應用程序中,我使用這個字節數組並使用這些字節通過內存流加載圖像。
問題是圖像降級了很多。
如果我將圖像保存到硬盤驅動器,然后將其轉換為客戶端的內存流,則圖像質量良好。
問題在於我將其作為內存流加載。
我把它編碼為jpeg。
如果我在發送到服務器之前將其編碼為PNG,則質量再次良好。 編碼為PNG的麻煩在字節數組中的大小就會出現問題。
我的意圖一直是減少我必須上傳的字節數以改善響應時間。
我做錯了什么還是不能做到這一點?
這是我的代碼:
Bitmap imgContainingDifference
= GetDiffFromEmgu(CurrentJpegImage, PreviousJpegImage);
using (System.IO.MemoryStream msIn = new System.IO.MemoryStream())
{
holding.Save(msIn, System.Drawing.Imaging.ImageFormat.Jpeg);
data = msIn.ToArray();
}
//test here
using (System.IO.MemoryStream msOut = new System.IO.MemoryStream(_data))
{
Bitmap testIMG = (Bitmap)Image.FromStream(msOut);
}
//result is image is poor/degrades
如果我這樣做:
using (System.IO.MemoryStream msIn = new System.IO.MemoryStream())
{
holding.Save(msIn, System.Drawing.Imaging.ImageFormat.Png);
data = msIn.ToArray();
}
using (System.IO.MemoryStream msOut = new System.IO.MemoryStream(_data))
{
Bitmap testIMG = (Bitmap)Image.FromStream(msOut);
}
//Image is good BUT the size of the byte array is
//10 times the size of the CurrentFrame right at the start.
這是使用孩子建議時圖像的樣子:
我現在嘗試使用來自@MagnatLU的類型建議的編碼器,如果我使用FreeImage.Net,我也可以獲得相同質量的圖像。
您可以在將文件編碼為值時設置JPEG壓縮級別 ,這是質量和大小之間最佳的經驗權衡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.