2012-11-01 11 views
0

Adjust the contrast of an image in C# efficiently對比度的圖像對上述問題

解決方案並不在VB中工作2005年

我需要在VB2005

的解決方案,這是低於

公共靜態位圖AdjustContrast C#代碼(位圖圖像​​,浮點值)

{

Value = (100.0f + Value)/100.0f; 
Value *= Value; 
Bitmap NewBitmap = (Bitmap)Image.Clone(); 
BitmapData data = NewBitmap.LockBits(
    new Rectangle(0, 0, NewBitmap.Width, NewBitmap.Height), 
    ImageLockMode.ReadWrite, 
    NewBitmap.PixelFormat); 

unsafe 
{ 
    for (int y = 0; y < NewBitmap.Height; ++y) 
    { 
     byte* row = (byte*)data.Scan0 + (y * data.Stride); 
     int columnOffset = 0; 
     for (int x = 0; x < NewBitmap.Width; ++x) 
     { 
      byte B = row[columnOffset]; 
      byte G = row[columnOffset + 1]; 
      byte R = row[columnOffset + 2]; 

      float Red = R/255.0f; 
      float Green = G/255.0f; 
      float Blue = B/255.0f; 
      Red = (((Red - 0.5f) * Value) + 0.5f) * 255.0f; 
      Green = (((Green - 0.5f) * Value) + 0.5f) * 255.0f; 
      Blue = (((Blue - 0.5f) * Value) + 0.5f) * 255.0f; 

      int iR = (int)Red; 
      iR = iR > 255 ? 255 : iR; 
      iR = iR < 0 ? 0 : iR; 
      int iG = (int)Green; 
      iG = iG > 255 ? 255 : iG; 
      iG = iG < 0 ? 0 : iG; 
      int iB = (int)Blue; 
      iB = iB > 255 ? 255 : iB; 
      iB = iB < 0 ? 0 : iB; 

      row[columnOffset] = (byte)iB; 
      row[columnOffset + 1] = (byte)iG; 
      row[columnOffset + 2] = (byte)iR; 

      columnOffset += 4; 
     } 
    } 
} 

NewBitmap.UnlockBits(data); 

return NewBitmap; 

}

&這裏是VB2005代碼

公共共享功能AdjustContrast(圖像作爲位圖,值作爲單)作爲位圖

Value = (100F + Value)/100F 
Value *= Value 
Dim NewBitmap As Bitmap = DirectCast(Image.Clone(), Bitmap) 

Dim data As BitmapData = NewBitmap.LockBits(New Rectangle(0, 0, NewBitmap.Width, NewBitmap.Height), ImageLockMode.ReadWrite, NewBitmap.PixelFormat) 

For y As Integer = 0 To NewBitmap.Height - 1 
    Dim row As Pointer(Of Byte) = CType(data.Scan0, Pointer(Of Byte)) + (y * data.Stride) 
    Dim columnOffset As Integer = 0 
    For x As Integer = 0 To NewBitmap.Width - 1 
     Dim B As Byte = row(columnOffset) 
     Dim G As Byte = row(columnOffset + 1) 
     Dim R As Byte = row(columnOffset + 2) 

     Dim Red As Single = R/255F 
     Dim Green As Single = G/255F 
     Dim Blue As Single = B/255F 
     Red = (((Red - 0.5F) * Value) + 0.5F) * 255F 
     Green = (((Green - 0.5F) * Value) + 0.5F) * 255F 
     Blue = (((Blue - 0.5F) * Value) + 0.5F) * 255F 

     Dim iR As Integer = CInt(Red) 
     iR = If(iR > 255, 255, iR) 
     iR = If(iR < 0, 0, iR) 
     Dim iG As Integer = CInt(Green) 
     iG = If(iG > 255, 255, iG) 
     iG = If(iG < 0, 0, iG) 
     Dim iB As Integer = CInt(Blue) 
     iB = If(iB > 255, 255, iB) 
     iB = If(iB < 0, 0, iB) 

     row(columnOffset) = CByte(iB) 
     row(columnOffset + 1) = CByte(iG) 
     row(columnOffset + 2) = CByte(iR) 

     columnOffset += 4 
    Next 
Next 

NewBitmap.UnlockBits(data) 

Return NewBitmap 

端功能

昏暗行作爲指針(字節)= CType(data.Scan0,指針(字節))+(y * data.Stride)

上述行給出錯誤,因爲VB不支持

昏暗行作爲指針(字節)

+0

當代碼從C#轉換爲VB.NET它給指針錯誤 – Dandy

+0

沒有看到你有它不可能說如何解決您收到錯誤的VB代碼。您可以嘗試使用此工具轉換代碼,http://www.developerfusion.com/tools/convert/csharp-to-vb/。我用它來做簡單的事情,似乎運作良好。 – Kratz

+0

@Kratz我已經過了代碼vb中的代碼給出指針錯誤(字節) – Dandy

回答

2

這是我在嘗試轉換。我正在使用IntPtr數據結構來處理持有未指定的指針,並執行指針添加以獲取每行的IntPtr。然後,當它通過該行時,我使用Marshal.ReadByteMarshal.WriteByte方法來處理讀取和寫入非託管數據。我測試過,它似乎工作。

value = (100.0F + value)/100.0F 
    value *= value 
    Dim NewBitmap As Bitmap = DirectCast(Image.Clone(), Bitmap) 

    Dim data As BitmapData = NewBitmap.LockBits(New Rectangle(0, 0, NewBitmap.Width, NewBitmap.Height), ImageLockMode.ReadWrite, NewBitmap.PixelFormat) 

    For y As Integer = 0 To NewBitmap.Height - 1 

     Dim RowPtr = IntPtr.Add(data.Scan0, y * data.Stride) 

     Dim columnOffset As Integer = 0 
     For x As Integer = 0 To NewBitmap.Width - 1 
      Dim B As Byte = System.Runtime.InteropServices.Marshal.ReadByte(RowPtr, columnOffset) 
      Dim G As Byte = System.Runtime.InteropServices.Marshal.ReadByte(RowPtr, columnOffset + 1) 
      Dim R As Byte = System.Runtime.InteropServices.Marshal.ReadByte(RowPtr, columnOffset + 2) 

      Dim Red As Single = R/255.0F 
      Dim Green As Single = G/255.0F 
      Dim Blue As Single = B/255.0F 
      Red = (((Red - 0.5F) * value) + 0.5F) * 255.0F 
      Green = (((Green - 0.5F) * value) + 0.5F) * 255.0F 
      Blue = (((Blue - 0.5F) * value) + 0.5F) * 255.0F 

      Dim iR As Integer = CInt(Red) 
      iR = If(iR > 255, 255, iR) 
      iR = If(iR < 0, 0, iR) 
      Dim iG As Integer = CInt(Green) 
      iG = If(iG > 255, 255, iG) 
      iG = If(iG < 0, 0, iG) 
      Dim iB As Integer = CInt(Blue) 
      iB = If(iB > 255, 255, iB) 
      iB = If(iB < 0, 0, iB) 

      System.Runtime.InteropServices.Marshal.WriteByte(RowPtr, columnOffset, CByte(iB)) 
      System.Runtime.InteropServices.Marshal.WriteByte(RowPtr, columnOffset + 1, CByte(iG)) 
      System.Runtime.InteropServices.Marshal.WriteByte(RowPtr, columnOffset + 2, CByte(iR)) 

      columnOffset += 4 
     Next 

    Next 

    NewBitmap.UnlockBits(data) 
+0

對於遲到的反饋感到抱歉,你的代碼似乎已經寫入vs2010,我在使用vs2005,vs2005不允許IntPtr.Add方法 – Dandy