-2

I am using the following code to copy from ms chart image to clipboard. But the image copied is in clip board is too large. Each of them becomes 3 MB. Is there any way to image compress the bitmap object? Not file compress but image compress like jpeg compress

using (MemoryStream ms = new MemoryStream())
{
    chart1.SaveImage(ms, ChartImageFormat.Png);
    Bitmap bm = new Bitmap(ms);
    Clipboard.SetImage(bm);
}
Yousuf Hossain
  • 172
  • 1
  • 12
  • Why do you say that it is "too large"? Is this causing an error? – Paul Williams Oct 14 '14 at 13:11
  • Yes, there are a few libraries: https://www.google.co.uk/search?sourceid=chrome-psyapi2&ion=1&espv=2&ie=UTF-8&q=compress%20bitmap%20c%23 –  Oct 14 '14 at 13:14
  • try this msdn thread https://social.msdn.microsoft.com/Forums/vstudio/en-US/5e755b7b-c0f3-46e3-b4cd-5881bf654d7f/c-convert-bitmap-to-bytes-or-high-compress – Amorphis Oct 14 '14 at 13:15
  • There is no error. But it is very inconvenient for engineers to send over several 3MB plots. I need to copy a low quality around 500 kb image to clipboard. – Yousuf Hossain Oct 14 '14 at 14:11
  • If this is for production code, you may want to think about what would happen if the user already has something on the clipboard that they wouldn't be too happy with your program trampling over... – Steve Oct 15 '14 at 17:39
  • I pasted in word document then copied again and the size of the image reduced quite a bit. This is a commercial product. I will code a pop up informing users that existing clipboard will be replaced by the image. – Yousuf Hossain Oct 16 '14 at 14:34

1 Answers1

0

You are saving the image to clipboard with png format which is a loss-less compressed image format. They can reach large file size depending on the image data saved.

In your code change the image format to jpeg as below to reduce the image size.

chart1.SaveImage(ms, ChartImageFormat.Jpeg);
Junaith
  • 3,298
  • 24
  • 34
  • I have tried Jpeg as well but does not compress. There compression modes for Jpeg. I need a code that does that compression – Yousuf Hossain Oct 16 '14 at 12:43
  • @YousufHossain - the compression depends on your image... depending on the image data you might not see much difference between compressed and uncompressed data... – Junaith Oct 16 '14 at 13:00