-2

If I copy a region from Photoshop in RGB 8 bits the Clipboard.GetImage() has a black and white image, but if I change it to an indexed mode (256) GetImage returns a colored image.

I've also tried with the GetDataObject method, but with same results. It seems the image format is memoryBMP but in RGB/8 format so I can't get the full colored image.

How to retrieve color image from Photoshop clipboard format back into my C# app?

This is the code I'm using. The image is saved, but..

  • If I copy pixels from an image in Photoshop mode RGB/8 it save as b/w.

  • If mode is index (256 colors) it saves color.

The image format in data is of type :

[ImageFormat: b96b3caa-0728-11d3-9d7b-0000f81ef32e] MemoryBMP

Following is my C# code...

IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Dib))
{
    Bitmap image = (System.Drawing.Image)data.GetData(DataFormats.Dib, false) as Bitmap;

    var encoder = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == ImageFormat.Jpeg.Guid);
    var encParams = new EncoderParameters(1);
    encParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 24L);
    image.Save("encode.jpg", encoder, encParams);
}
8
  • I did not downvote but I think you must show some testable code to allow others to re-create and/or fix your problem... PS: Remember RGB 8-bit is really a total of 24 bits colour (since 8 bits for red + 8 for green + 8 for blue). If that helps you in some way.
    – VC.One
    Oct 5, 2017 at 9:53
  • 1
    Thanks for your answer. My first post, learning the rules...
    – alexeef
    Oct 5, 2017 at 9:59
  • There is a small edit button under your post. Or else just use link (add /edit to page url) : stackoverflow.com/posts/46576473/edit
    – VC.One
    Oct 5, 2017 at 11:53
  • Test my answer and if it works for you, then mark as solved to help others who need this correct answer too. Let me know how it goes...
    – VC.One
    Oct 5, 2017 at 18:40
  • Actually the problem seems to be the actual data comes as BGRA instead of ARGB, and I forget to mention I'm doing this in Unity3D
    – alexeef
    Oct 6, 2017 at 22:22

1 Answer 1

0

If you're trying to just select a rectangle area in Photoshop (or any image program, like Paint etc) then paste that into your app (via get from Clipboard)...

Try it like this :

//# Get copied data (image) from clipboard
IDataObject data = Clipboard.GetDataObject();

if (data.GetDataPresent(DataFormats.Dib))
{
    //# Make into C# bitmap
    Bitmap image = (data.GetData(DataFormats.Bitmap,true) as Bitmap);

    //# Show in some PictureBox
    //pbx.Image = image;

    //# Save to disk
    image.Save("c:/test//encode_test1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
  • Compile and run your app (its window should be open).
  • In Photoshop.. open an image and select/copy an area.

  • In your app, get the image brought in (I used keypress to run function of above code).

  • Find the JPG file at C:\ drive inside the test folder.

  • If you created aPictureBox object you can even view "selected area" image within your app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.