2011-01-24 17 views
17

मेरे पास एक ऐसा फॉर्म है जिसमें एक छवि है। मैं छवि की अस्पष्टता को बदलने के लिए एक स्लाइडर का उपयोग कर रहा हूँ। तो स्लाइडर की "ValueChanged" घटना में मैं अस्पष्टता को बदलने के लिए निम्न विधि को कॉल कर रहा हूं।बिटमैप छवि की अस्पष्टता बदलना

//Setting the opacity of the image 
public static Image SetImgOpacity(Image imgPic, float imgOpac) 
{ 
    Bitmap bmpPic = new Bitmap(imgPic.Width, imgPic.Height); 
    Graphics gfxPic = Graphics.FromImage(bmpPic); 

    ColorMatrix cmxPic = new ColorMatrix(); 
    cmxPic.Matrix33 = imgOpac; 
    ImageAttributes iaPic = new ImageAttributes(); 
    iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); 
    gfxPic.DrawImage(imgPic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic); 
    gfxPic.Dispose();    

    return bmpPic; 
} 

लौटाई गई छवि मूल छवि पर सेट है।

मेरे समस्या यह है कि छवि की पारदर्शिता नहीं बदल रहा है ... अगर वहाँ है किसी भी त्रुटि बताते .. Thnx तरह पर्याप्त हो कृपया ...

उत्तर

35

CodeProject से एक का प्रयास करें - Change Opacity of Image in C#:

/// <summary> 
/// method for changing the opacity of an image 
/// </summary> 
/// <param name="image">image to set opacity on</param> 
/// <param name="opacity">percentage of opacity</param> 
/// <returns></returns> 
public Image SetImageOpacity(Image image, float opacity) 
{ 
    try 
    { 
     //create a Bitmap the size of the image provided 
     Bitmap bmp = new Bitmap(image.Width, image.Height); 

     //create a graphics object from the image 
     using (Graphics gfx = Graphics.FromImage(bmp)) { 

      //create a color matrix object 
      ColorMatrix matrix = new ColorMatrix();  

      //set the opacity 
      matrix.Matrix33 = opacity; 

      //create image attributes 
      ImageAttributes attributes = new ImageAttributes();  

      //set the color(opacity) of the image 
      attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);  

      //now draw the image 
      gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); 
     } 
     return bmp; 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
     return null; 
    } 
} 
+0

पैरामीटर अस्पष्टता के लिए आपकी टिप्पणी "अस्पष्टता का प्रतिशत" कहती है, लेकिन यह प्रतिशत नहीं है, यह एक पूर्ण कारक है, यानी, 0.0 से 1.0 तक। – RenniePet

+0

मुझे खुदाई के कुछ घंटे लगे, लेकिन यह वह पोस्ट था जिसने आखिरकार मेरे लिए इसका उत्तर दिया। धन्यवाद! – samuelesque

+5

गणितीय रूप से 65% और 0.65 के बीच कोई अंतर नहीं है। वे बराबर हैं। –

2

मैं ImageAttributes दृष्टिकोण से परिचित नहीं हूँ, लेकिन आप छवि के सभी पिक्सल के माध्यम से बस पिक्सल के रंग के अल्फा घटक को संशोधित करने में सक्षम होना चाहिए।

+1

मेरे पास कुछ स्पष्ट करने के लिए कुछ है। अगर मैं सभी पिक्सेल से गुजरना चाहता हूं तो यह समय का उपभोग करेगा। कोई सीधा रास्ता ?? तत्काल उत्तर के लिए Thnx – JCTLK

+1

@JCTLK: बेशक यह होगा। यह छोटी छवियों पर बहुत तेजी से किया जा सकता है। – leppie

14

आप पाश पिक्सल से अधिक और केवल अल्फ़ा चैनल खेलते हैं। यदि आप बिटमैप के साथ ऐसा करते हैं। लॉकबिट्स यह वास्तव में बहुत तेज़ होगा।

private const int bytesPerPixel = 4; 

    /// <summary> 
    /// Change the opacity of an image 
    /// </summary> 
    /// <param name="originalImage">The original image</param> 
    /// <param name="opacity">Opacity, where 1.0 is no opacity, 0.0 is full transparency</param> 
    /// <returns>The changed image</returns> 
    public static Image ChangeImageOpacity(Image originalImage, double opacity) 
    { 
     if ((originalImage.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed) 
     { 
      // Cannot modify an image with indexed colors 
      return originalImage; 
     } 

     Bitmap bmp = (Bitmap)originalImage.Clone(); 

     // Specify a pixel format. 
     PixelFormat pxf = PixelFormat.Format32bppArgb; 

     // Lock the bitmap's bits. 
     Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); 
     BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, pxf); 

     // Get the address of the first line. 
     IntPtr ptr = bmpData.Scan0; 

     // Declare an array to hold the bytes of the bitmap. 
     // This code is specific to a bitmap with 32 bits per pixels 
     // (32 bits = 4 bytes, 3 for RGB and 1 byte for alpha). 
     int numBytes = bmp.Width * bmp.Height * bytesPerPixel; 
     byte[] argbValues = new byte[numBytes]; 

     // Copy the ARGB values into the array. 
     System.Runtime.InteropServices.Marshal.Copy(ptr, argbValues, 0, numBytes); 

     // Manipulate the bitmap, such as changing the 
     // RGB values for all pixels in the the bitmap. 
     for (int counter = 0; counter < argbValues.Length; counter += bytesPerPixel) 
     { 
      // argbValues is in format BGRA (Blue, Green, Red, Alpha) 

      // If 100% transparent, skip pixel 
      if (argbValues[counter + bytesPerPixel - 1] == 0) 
       continue; 

      int pos = 0; 
      pos++; // B value 
      pos++; // G value 
      pos++; // R value 

      argbValues[counter + pos] = (byte) (argbValues[counter + pos] * opacity); 
     } 

     // Copy the ARGB values back to the bitmap 
     System.Runtime.InteropServices.Marshal.Copy(argbValues, 0, ptr, numBytes); 

     // Unlock the bits. 
     bmp.UnlockBits(bmpData); 

     return bmp; 
    } 
+2

यह सबसे अच्छा होना चाहिए का जवाब! – buzznfrog

+0

एक killjoy होने से नफरत है, लेकिन यदि आप बाद में परिणाम का निपटान कर रहे हैं तो छवि को संशोधित नहीं किया जा सकता है। लेकिन महान मदद जनवरी। धन्यवाद :) – Czeshirecat

-1

ImageAttributes विधि पीएनजी के साथ ठीक से काम करेंगे के रूप में मूल पोस्ट में सूचीबद्ध किया है, लेकिन जेपीईजी के लिए यदि आपके पास रंगीन पहले साथ ग्राफिक्स कैनवास को भरने बाढ़ की जरूरत है। चूंकि यह एक छोटी अवांछित सीमा छोड़ सकता है, केवल अगर यह अस्पष्टता 1.0 से कम है:

if(opacity < 1.0) 
{ 
    // g is a Graphics object 
    g.Clear(Color.White); 
} 
// set color matrix and draw image as shown in other posts 
// ... 
संबंधित मुद्दे