2012-07-11 104 views
4

我創建它畫在我的基地png圖片,從基礎,我可以保存爲PNG圖像,供大家參考如何創建反向PNG圖像?

Graphics g = e.Graphics; 
.... 
g.DrawLine(pen, new Point(x, y), new Point(x1, y1)); 
..... 
base.OnPaint(e); 

using (var bmp = new Bitmap(500, 50)) 
{ 
    base.DrawToBitmap(bmp, new Rectangle(0, 0, 500, 50)); 
    bmp.Save(outPath); 
} 

這是單色透明圖像,現在我怎麼能逆這個形象像PNG充滿任何顏色,真實圖像部分應該是透明的,有沒有可能性?

詳細一點:如此透明會去不透明,哪裏有補會去透明

+0

我使用的載體,使極快的代碼 [檢查我的崗位更多信息](https://stackoverflow.com/a/44467764/6176081) – 2017-06-09 23:09:37

回答

5

編輯(thaks爲Thomas符號)

public void ApplyInvert() 
{ 
    byte A, R, G, B; 
    Color pixelColor; 

    for (int y = 0; y < bitmapImage.Height; y++) 
    { 
     for (int x = 0; x < bitmapImage.Width; x++) 
     { 
      pixelColor = bitmapImage.GetPixel(x, y); 
      A = (byte)(255 - pixelColor.A); 
      R = pixelColor.R; 
      G = pixelColor.G; 
      B = pixelColor.B; 
      bitmapImage.SetPixel(x, y, Color.FromArgb((int)A, (int)R, (int)G, (int)B)); 
     } 
    } 
} 

從這裏:Image Processing in C#: Inverting an image

+0

涼爽快速反應,讓我試試這個,感謝利雅 – manny 2012-07-11 08:49:21

+0

如此接近! XD但請閱讀帖子的最後一句話! – 2012-07-11 08:50:31

+0

不,我認爲這是反轉顏色(負),我的問題是反轉透明的圖像。所以透明將不透明,並且填充的位置將變爲透明 – manny 2012-07-11 08:54:59

6

如果您願意使用unsafe代碼,則有更快捷的方法:

private unsafe void Invert(Bitmap bmp) 
{ 
    int w = bmp.Width, h = bmp.Height; 
    BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); 

    int* bytes = (int*)data.Scan0; 
    for (int i = w*h-1; i >= 0; i--) 
     bytes[i] = ~bytes[i]; 
    bmp.UnlockBits(data); 
} 

請注意,這並不關心顏色,也會反轉這些顏色。如果你想使用特定的顏色,那麼代碼將不得不稍微修改。

+0

什麼是不安全的? – manny 2012-07-11 08:58:58

+2

找到了一個鏈接。基本上它是一個使用指針的代碼(就像那個'int *'那樣)。 .NET運行時無法驗證此代碼不會執行某些愚蠢的操作,如編寫未分配的內存,因此名稱爲「不安全」。因爲如果你不小心,它會損壞程序存儲器。爲了編譯這段代碼,你必須在項目選項中啓用不安全的代碼(默認情況下它是關閉的),並且我認爲也有一些安全隱患(不安全的代碼在部分信任的程序集或其他東西中不起作用)。 – 2012-07-11 09:08:09

+0

你顯示了一個有效的分數,是的,這是很好的檢查。 – manny 2012-07-11 09:11:13

0

當你說反轉透明部分爲彩色,你是否在PNG圖像中存儲真正的顏色只是設置爲完全透明?很多程序都會通過從透明度中移除顏色數據來優化png,所以您不能將其反轉。

顏色可以轉換爲透明度 但透明度(無底色)不能轉換爲顏色。

如果您的幸運PNG未經過優化,仍然保留原始顏色數據,但如果您從用戶輸入中完成此操作,那麼它將不適用於高比例的病例。

+0

是啊,我的是單色圖像,要麼是黑色要麼是白色,所以反色會有相同的顏色,我擔心alpha – manny 2012-07-11 10:39:18

5

誰想要反轉的位圖顏色不使用不安全的一個快速方法:

public static void BitmapInvertColors(Bitmap bitmapImage) 
{ 
    var bitmapRead = bitmapImage.LockBits(new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb); 
    var bitmapLength = bitmapRead.Stride * bitmapRead.Height; 
    var bitmapBGRA = new byte[bitmapLength]; 
    Marshal.Copy(bitmapRead.Scan0, bitmapBGRA, 0, bitmapLength); 
    bitmapImage.UnlockBits(bitmapRead); 

    for (int i = 0; i < bitmapLength; i += 4) 
    { 
     bitmapBGRA[i]  = (byte)(255 - bitmapBGRA[i]); 
     bitmapBGRA[i + 1] = (byte)(255 - bitmapBGRA[i + 1]); 
     bitmapBGRA[i + 2] = (byte)(255 - bitmapBGRA[i + 2]); 
     //  [i + 3] = ALPHA. 
    } 

    var bitmapWrite = bitmapImage.LockBits(new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb); 
    Marshal.Copy(bitmapBGRA, 0, bitmapWrite.Scan0, bitmapLength); 
    bitmapImage.UnlockBits(bitmapWrite); 
} 

位圖GetPixel和SetPixel是的極端緩慢,這種方法通過位圖像素複製到一個字節數組,你可以然後循環並更改,最終將像素複製回來。