文章出處

目前操作位圖的主流方法有三種:

 

  1、基于Bitmap像素的處理方法,以GetPixel()和SetPixel()方法為主。方法調用簡單,但是效率偏低。

  2、基于內存的像素操作方法,以System.Runtime.InteropServices.Marshal.Copy()方法將數據變為非托管資源,操作后再寫入內存。

  3、基于指針的操作方式,效率最高,但是對使用者的能力有要求,能力不夠者容易造成內存溢出。

 

 

第二種方法的一個實例:

 1 //大圖逐行遍歷,y為行索引
 2             for (var y = 0; y < destHeight; y++)
 3             {
 4                 //小圖。把一行數據讀入數組。第一個參數是起始位。
 5                 System.Runtime.InteropServices.Marshal.Copy(srcScan0 + y * srcStride, srcBuffer, 0, srcStride);
 6                 //大圖。
 7                 System.Runtime.InteropServices.Marshal.Copy(dstScan0 + y * dstStride, dstBuffer, 0, dstStride);
 8 
 9                 //大圖逐列,rgb三色.(與源碼相比做了修改,遍歷長度減小,數值因為是灰度值,計算了變為了三分之一。)
10                 for (var x = 0; x < destWidth; x ++)
11                 {
12                     //字節總索引
13                     int fullIndex = 3*x;
14                     //相乘,再除以255。返回一個byte
15                     //dstBuffer[x] = channelProcessFunction(ref srcBuffer[x], ref dstBuffer[x]);
16                     var blendValue = channelProcessFunction(ref srcBuffer[fullIndex], ref dstBuffer[fullIndex]);
17                     dstBuffer[fullIndex + 2] = blendValue;
18                     dstBuffer[fullIndex + 1] = blendValue;
19                     dstBuffer[fullIndex] = blendValue;
20                 }
21                 //寫回托管內存
22                 System.Runtime.InteropServices.Marshal.Copy(dstBuffer, 0, dstScan0 + y * dstStride, dstStride);
23             }
基于內存的像素操作

 

 

 

而對于灰度圖的處理,可以使用調整gamma值的方式。對于灰度圖處理的一個實例:

 1 /// <summary>
 2         /// 把小圖片按照權重,重設gamma值,重新渲染。可得到加權重后的小圖,背景為白色,非透明。
 3         /// </summary>
 4         /// <param name="image">小圖片</param>
 5         /// <param name="weight">權重</param>
 6         /// <returns>加權重后的小圖,背景為白色,非透明</returns>
 7         private static Bitmap ApplyHeatValueToImage(Bitmap image, float weight)
 8         {
 9             //新建臨時位圖
10             var tempImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb);
11 
12             using (var g = Graphics.FromImage(tempImage))
13             {
14                 //把權重參數映射為[0.1-5],以便后面進行gamma的矯正。gamma值正常范圍為1.0 - 2.2
15                 //此處拓展,可以讓色彩更加intense
16                 //gamma值可用來進行灰度值層面的明暗矯正,改善失真。
17                 ////I want to make the color more intense (White/bright)
18                 if (weight < 0.02f) weight = 0.02f;//最小0.02
19                 weight *= 5f;
20                 if (weight > 5f) weight = 5f;
21 
22                 // Create ImageAttributes
23                 var ia = new ImageAttributes();
24 
25                 //Gamma values range from 0.1 to 5.0 (normally 0.1 to 2.2), with 0.1 being the brightest and 5.0 the darkest.
26                 //Convert the 100% to a range of 0.1-5 by multiplying it by 5
27                 ia.SetGamma(weight, ColorAdjustType.Bitmap);
28 
29                 //在image中 重繪
30                 // Draw Image with the attributes
31                 g.DrawImage(image,
32                             new Rectangle(0, 0, image.Width, image.Height),//這里如果size設小一點,可以對目標圖像進行縮放。如果小于Graphics的尺寸,則會出現白邊
33                             0, 0, image.Width, image.Height,//這里可以對源圖像進行裁剪。
34                             GraphicsUnit.Pixel, ia);
35             }
36             //New dot with a different intensity
37             return tempImage;
38         }

 


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 AutoPoster 的頭像
    AutoPoster

    互聯網 - 大數據

    AutoPoster 發表在 痞客邦 留言(0) 人氣()