Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. An other image processing

An other image processing

Scheduled Pinned Locked Moved C#
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sajjad Izadi
    wrote on last edited by
    #1

    hi again friends, how can i change my image opacity?

    L 1 Reply Last reply
    0
    • S Sajjad Izadi

      hi again friends, how can i change my image opacity?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      One way would be to change the pixel format of the image to ARGB and then save it as PNG or TIFF file, like that:

      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;
      }

      using (Image img = Image.FromFile(filename))
      {
      Image img2 = ((Bitmap)img).Clone(new Rectangle(0, 0, img.Width, img.Height), PixelFormat.Format32bppArgb);
      img2 = SetImgOpacity(img2, 0.5f);
      img2.Save(filename2, ImageFormat.Png);
      img2.Dispose();
      }

      I got the SetImgOpacity method from here[^]. I just tested this code and it works fine. regards

      S 1 Reply Last reply
      0
      • L Lost User

        One way would be to change the pixel format of the image to ARGB and then save it as PNG or TIFF file, like that:

        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;
        }

        using (Image img = Image.FromFile(filename))
        {
        Image img2 = ((Bitmap)img).Clone(new Rectangle(0, 0, img.Width, img.Height), PixelFormat.Format32bppArgb);
        img2 = SetImgOpacity(img2, 0.5f);
        img2.Save(filename2, ImageFormat.Png);
        img2.Dispose();
        }

        I got the SetImgOpacity method from here[^]. I just tested this code and it works fine. regards

        S Offline
        S Offline
        Sajjad Izadi
        wrote on last edited by
        #3

        you're really great, merci

        L 1 Reply Last reply
        0
        • S Sajjad Izadi

          you're really great, merci

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          What I just tried out which works, you can also convert the image to grayscale with the ColorMatrix class used above, just set the matrix to

          cmxPic.Matrix00 = 0.299f;
          cmxPic.Matrix01 = 0.299f;
          cmxPic.Matrix02 = 0.299f;

          cmxPic.Matrix10 = 0.587f;
          cmxPic.Matrix11 = 0.587f;
          cmxPic.Matrix12 = 0.587f;

          cmxPic.Matrix20 = 0.114f;
          cmxPic.Matrix21 = 0.114f;
          cmxPic.Matrix22 = 0.114f;

          This will produce a grayscale image with a different approach as presented in the aricles. Hope this helps :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups