locked
picture changes after saving RRS feed

  • Question

  • hello i'm using following code for producing an effect in image
    ColorDialog cld = new ColorDialog();
                cld.ShowDialog();
                Color basecolor = cld.Color;
                Bitmap grayimg = new Bitmap(_imgedt.Image, _imgedt.Image.Width, _imgedt.Image.Height);
                for (int i = 0; i < _imgedt.Image.Width; i++)
                {
                    for (int j = 0; j < _imgedt.Image.Height; j++)
                    {
                        Color col = grayimg.GetPixel(i, j);
                        int graycol = (int)(col.R*.3  + col.G*.1 + col.B*.59 );
                        Color newcol = Color.FromArgb(graycol, basecolor);
                        grayimg.SetPixel(i, j, newcol);
                    }

                }
                _imgedt.Image = (Image)grayimg;
                Clipboard.SetImage(_imgedt.Image);

    after this when i take picture from clip board the picture is different and when i save the picture the picture become simply colored by basecolor.
    how to get the picture this code have produced?
    Sunday, February 28, 2010 5:34 AM

Answers

  • I should have said:  All 3 images look the same.  The clipboard image is RGB multiplied by the alpha channel.  You can save it as JPEG and it retains the shading.
    • Marked as answer by Riquel_Dong Friday, March 5, 2010 6:06 AM
    Sunday, February 28, 2010 8:40 PM

All replies

  • Works fine on Vista 64.  All 3 images are the same.
    Sunday, February 28, 2010 7:53 AM
  • i'm using Xp sp2 32bit. how does it depends on OS?
    Sunday, February 28, 2010 12:19 PM
  • I believe the differences you see are due to your use of an alpha channel.

    Although you use the SetImage method to add your 'grayimg' - which has an alpha channel - to the clipboard, if you try reading the image back again, it gets created as 32bpp RGB (i.e. no alpha channel any more).

    Try this ...

    // grayimg.PixelFormat = 32bppArgb (so it has the alpha channel).
    Clipboard.SetImage(grayimg);
    
    // Load the image from the clipboard.
    // Have a look at newImage.PixelFormat. It is now 32bppRgb (no alpha channel).
    Image newImage = (Image)Clipboard.GetImage();

    As for the appearance of only the background color when you save your image, this depends on the format used for saving.

    If the format in which you save the image doesn't support transparency (e.g. JPEG images), you will just see the background color.

    So,

    // OK. Bmp supports alpha channel.
    grayimg.Save(@"grayimg.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
    
    // Not OK. JPEG doesn't support alpha channel.
    grayimg.Save(@"grayimg.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

    Hope this helps. (Also, I don't know if this will vary by OS.)
    Sunday, February 28, 2010 8:14 PM
  • I should have said:  All 3 images look the same.  The clipboard image is RGB multiplied by the alpha channel.  You can save it as JPEG and it retains the shading.
    • Marked as answer by Riquel_Dong Friday, March 5, 2010 6:06 AM
    Sunday, February 28, 2010 8:40 PM