I have bitmap residing in memory (coming from my webcam but I don't think that this makes a difference. It is 960x540 120dpi
我有位图驻留在内存中(来自我的网络摄像头,但我不认为这有所不同。它是960x540 120dpi
you see that the picture in the lower part gets till the point where my shirt begins.
你看到下半部分的图片一直到我的衬衫开始的地方。
I know the bmp dimensions since I put this code prior resize
我知道bmp尺寸因为我在调整大小之前放了这个代码
using (var fileStream = new FileStream(@"C:\temp\3.bmp", FileMode.Create))
{
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(fileStream);
}
and the result is the picture above.
结果如上图所示。
Then I resize it with that:
然后我用它调整大小:
var resizedImage = new RenderTargetBitmap(
width, height, // Resized dimensions 200x112
source.DpiX, source.DpiY,// Default DPI values
PixelFormats.Default); // Default pixel format
and the result is the bmp below which is properly 200x112 but it cuts out in part of the image in the lower and right part.
结果是下面的bmp恰好是200x112,但它在部分图像的下部和右部切出。
I have seen that the problem is related with the dpi value in the RenderTargetBitmap instruction. If I divide the dpi by 1.25 everything gets fine but why 1.25???????
我已经看到问题与RenderTargetBitmap指令中的dpi值有关。如果我将dpi除以1.25,一切都会好起来,但为什么1.25 ???????
Thank you in advance for any help Patrick
提前感谢Patrick的任何帮助
--ADD--
- 加 -
There is something additional that I can't understand: I know the initial bitmap size for I have saved it to filesytem with the instructions above. But if I look at the properties by putting a breakpoint I see:
还有一些我无法理解的内容:我知道我的初始位图大小已经按照上面的说明将它保存到filesytem。但如果我通过设置断点来查看属性,我会看到:
BITMAP BEFORE RESIZE
BITMAP在调整之前
width,height = 768, 432
宽度,高度= 768,432
pixelwidth, pixelheight = 960, 540
pixelwidth,pixelheight = 960,540
dpiX, dpiY= 120, 120
dpiX,dpiY = 120,120
BITMAP AFTER RESIZE
BITMAP重新调整后
width,height = 160, 89
宽度,高度= 160,89
pixelwidth, pixelheight = 200, 112
pixelwidth,pixelheight = 200,112
dpiX, dpiY= 120, 120
dpiX,dpiY = 120,120
now I know that what counts here is pixelwidth, pixelheight so that's correct.
现在我知道这里重要的是像素宽度,像素高度,这是正确的。
If I do 960/786 I get 1.25! So that's my number but why? Can I correct the code as to make it a general solution???
如果我做960/786,我得到1.25!这是我的号码,但为什么呢?我可以更正代码,使其成为一般解决方案吗?
1 个解决方案
#1
0
Instead of a RenderTargetBitmap
you should simply use a TransformedBitmap
with an appropriate ScaleTransform
:
您应该使用具有适当ScaleTransform的TransformedBitmap而不是RenderTargetBitmap:
var scale = 200d / 960d;
var resizedImage = new TransformedBitmap(source, new ScaleTransform(scale, scale));
#1
0
Instead of a RenderTargetBitmap
you should simply use a TransformedBitmap
with an appropriate ScaleTransform
:
您应该使用具有适当ScaleTransform的TransformedBitmap而不是RenderTargetBitmap:
var scale = 200d / 960d;
var resizedImage = new TransformedBitmap(source, new ScaleTransform(scale, scale));