It looks like Bitmap.SetResolution() has no effect on clipboard, see the following trivial code:
Dim bitmap1 As Image = New System.Drawing.Bitmap(100, 100)
Using gr As Graphics = Graphics.FromImage(bitmap1)
gr.FillRectangle(System.Drawing.Brushes.Black, 0, 0, 99, 99)
gr.FillRectangle(System.Drawing.Brushes.White, 10, 10, 89, 89)
End Using
bitmap1.SetResolution(150, 150)
Clipboard.SetImage(bitmap1) ' DPI not set
bitmap1.Save("D:\bitmap1.png", imaging.ImageFormat.Png) ' DPI set
The file contains correctly set image DPI. In the clipbaord, it is not present.
Proof: In clipboard dump (image inserted into clipboard by IrfanView), see DIB bitmap header of 60 × 120
DPI (yellow=horizontal DPI, green=vertical DPI):
But after inserting image using .net's Clipboard.SetImage()
, both these numbers are 0
.
My goal: be able to paste image into Microsoft Word with proper size (taken from DPI and dimensions). Without DPI set in the clipboard, the image is too big after pasting. But it contains barcode already with 1 bar = 1 px resolution, so I cannot sample it down.
How to verify DPI: Either by clipboard viewer OR by opening the image in image editor which shows image properties. If you have only Word, drag&drop the image over the document. Image size of the above example should have been 1.69×1.69 cm – and if taken from file, it actually is. If from .NET-made clipboard, it isn't.
What I am missing in process of setting the image DPI?
(C# or VB, whatever you prefer.)