2015-10-31 25 views
1

林試圖加載由n個圖像n和通過複製每個像素爲m×m的製作在正米乘n米。我的意思是:如果圖像是:C#負載縮放的位圖

1 2 

3 4 

,m爲2,因此新的圖像將

1 1 2 2 

1 1 2 2 

3 3 4 4 

3 3 4 4 

到目前爲止,我只是做了明顯的方式:

Bitmap scaledImage = new Bitmap(image.Width * BeadWidth, image.Height * BeadHeight); 
    for (int w = 0; w < scaledImage.Width; ++w) { 
     for (int h = 0; h < scaledImage.Height; ++h) { 
      scaledImage.SetPixel(w, h, image.GetPixel(w/BeadWidth, h/BeadHeight)); 
    } 
    } 

但它需要很長時間。 我怎樣才能得到同樣的結果在更快的時間?

+1

1.不要做縮放自己時,使用'graphics.DrawImage'爲你縮放圖像,你可以明確地請求最近鄰居插值。 – Dai

+0

2.不要使用'GetPixel'和'SetPixel',他們是緩慢的。相反,使用'LockBits'操作'Bitmap'對象,並將數據複製到'Byte []'數組中(爲了安全起見),或者使用帶安全指針的'unsafe' C#快速迭代 - 在不復制的情況下操作位圖數據。 – Dai

回答

2

DrawImage(Image, Int32, Int32, Int32, Int32)
在指定的位置以指定的尺寸繪製指定的圖像。

Bitmap bmp = new Bitmap(width*2, height*2); 
Graphics graph = Graphics.FromImage(bmp); 
graph.InterpolationMode = InterpolationMode.High; 
graph.CompositingQuality = CompositingQuality.HighQuality; 
graph.SmoothingMode = SmoothingMode.AntiAlias; 
graph.DrawImage(image, new Rectangle(0, 0, width*2, height*2)); 

雖然這將是高品質的,我不認爲他實際上想要 抗鋸齒,插值結果。如果是這樣的話,正確的 設置是:

graph.InterpolationMode = InterpolationMode.NearestNeighbor; 
graph.SmoothingMode = SmoothingMode.None; 
+1

雖然這將是高品質的,我不認爲他真正想要的抗鋸齒,插值結果。如果是這樣的話,正確的設置應該是:'graph.InterpolationMode = InterpolationMode.NearestNeighbor; graph.SmoothingMode = SmoothingMode.None;' – TaW

+0

你可能是對的 – Abdullah

+0

第二是我想要的,完美工作 – MyNick