Web source: revert crop measurement to f39ebd9 scrollWidth/Height (nailed right/bottom) — canvas-size render untouched
- ContentBoundsScript back to scrollWidth/scrollHeight extents, kept as a real JS object
(the JSON.stringify double-encode bug that silently disabled the crop stays fixed).
- Crop again from (0,0), no X/Y offset. The union-of-elements approach (582d1f4) created an
all-sides gap by including invisible full-canvas wrappers; revert to the proven code.
This commit is contained in:
+13
-37
@@ -29,8 +29,6 @@ public sealed class WebView2Manager : IDisposable
|
||||
public VideoFrame? LatestFrame;
|
||||
public double ViewportCssW;
|
||||
public double ViewportCssH;
|
||||
public double ContentCssX;
|
||||
public double ContentCssY;
|
||||
public double ContentCssW;
|
||||
public double ContentCssH;
|
||||
public bool Disposed;
|
||||
@@ -165,33 +163,15 @@ public sealed class WebView2Manager : IDisposable
|
||||
// viewport for widgets that don't fill it — the leftover area is dead space
|
||||
// that must not stretch across the selection box. Crop the capture to the
|
||||
// content bounds (anchored at 0,0) so the box shows exactly the widget.
|
||||
// The widget's ACTUAL visible content bounds — the union of every visible
|
||||
// body rectangle (getBoundingClientRect). This is the real widget rect,
|
||||
// excluding the page's own margins/dead space, with its top-left offset.
|
||||
// A real object is returned (NOT JSON.stringify — ExecuteScriptAsync
|
||||
// JSON-encodes the result, which would double-encode a string).
|
||||
// This restores f39ebd9's measurement (scrollWidth/Height), which the
|
||||
// creator confirmed nailed the right and bottom of the box. The result is
|
||||
// returned as a real JS object, NOT JSON.stringify — ExecuteScriptAsync
|
||||
// JSON-encodes the result, so stringifying would double-encode and the
|
||||
// JSON parse would throw, silently disabling the crop.
|
||||
private const string ContentBoundsScript =
|
||||
"(function(){" +
|
||||
"var rs=[];var body=document.body;" +
|
||||
"if(body){var br=body.getBoundingClientRect();if(br.width||br.height)rs.push(br);}" +
|
||||
"var els=document.querySelectorAll('body *');" +
|
||||
"for(var i=0;i<els.length;i++){" +
|
||||
"var s=getComputedStyle(els[i]);" +
|
||||
"if(s&&s.display==='none')continue;" +
|
||||
"var r=els[i].getBoundingClientRect();" +
|
||||
"if(r.width||r.height)rs.push(r);" +
|
||||
"}" +
|
||||
"if(!rs.length)return null;" +
|
||||
"var l=rs[0].left,t=rs[0].top,rt=rs[0].right,bt=rs[0].bottom;" +
|
||||
"for(var j=1;j<rs.length;j++){" +
|
||||
"l=Math.min(l,rs[j].left);" +
|
||||
"t=Math.min(t,rs[j].top);" +
|
||||
"rt=Math.max(rt,rs[j].right);" +
|
||||
"bt=Math.max(bt,rs[j].bottom);" +
|
||||
"}" +
|
||||
"return {l:l,t:t,w:Math.max(1,rt-l),h:Math.max(1,bt-t)," +
|
||||
"vw:window.innerWidth||1,vh:window.innerHeight||1};" +
|
||||
"})()";
|
||||
"({w:document.documentElement.scrollWidth||document.body.scrollWidth||0," +
|
||||
"h:document.documentElement.scrollHeight||document.body.scrollHeight||0," +
|
||||
"vw:window.innerWidth||1,vh:window.innerHeight||1})";
|
||||
|
||||
private async Task QueryContentBoundsAsync(string sourceId)
|
||||
{
|
||||
@@ -205,8 +185,6 @@ public sealed class WebView2Manager : IDisposable
|
||||
var root = doc.RootElement;
|
||||
session.ViewportCssW = root.GetProperty("vw").GetDouble();
|
||||
session.ViewportCssH = root.GetProperty("vh").GetDouble();
|
||||
session.ContentCssX = root.GetProperty("l").GetDouble();
|
||||
session.ContentCssY = root.GetProperty("t").GetDouble();
|
||||
session.ContentCssW = root.GetProperty("w").GetDouble();
|
||||
session.ContentCssH = root.GetProperty("h").GetDouble();
|
||||
_ = CaptureFrame(sourceId);
|
||||
@@ -252,25 +230,23 @@ public sealed class WebView2Manager : IDisposable
|
||||
|
||||
var stride = pixW * 4;
|
||||
|
||||
int cropX = 0, cropY = 0, cropW = pixW, cropH = pixH;
|
||||
int cropW = pixW, cropH = pixH;
|
||||
if (session.ViewportCssW >= 1 && session.ViewportCssH >= 1 &&
|
||||
session.ContentCssW >= 1 && session.ContentCssH >= 1)
|
||||
{
|
||||
var sx = pixW / session.ViewportCssW;
|
||||
var sy = pixH / session.ViewportCssH;
|
||||
cropX = (int)Math.Clamp(Math.Round(session.ContentCssX * sx), 0, pixW - 1);
|
||||
cropY = (int)Math.Clamp(Math.Round(session.ContentCssY * sy), 0, pixH - 1);
|
||||
cropW = (int)Math.Clamp(Math.Round(session.ContentCssW * sx), 1, pixW - cropX);
|
||||
cropH = (int)Math.Clamp(Math.Round(session.ContentCssH * sy), 1, pixH - cropY);
|
||||
cropW = (int)Math.Clamp(Math.Round(session.ContentCssW * sx), 1, pixW);
|
||||
cropH = (int)Math.Clamp(Math.Round(session.ContentCssH * sy), 1, pixH);
|
||||
}
|
||||
|
||||
var outPixels = pixels;
|
||||
if (cropW != pixW || cropH != pixH || cropX != 0 || cropY != 0)
|
||||
if (cropW != pixW || cropH != pixH)
|
||||
{
|
||||
var dstStride = cropW * 4;
|
||||
outPixels = new byte[dstStride * cropH];
|
||||
for (var r = 0; r < cropH; r++)
|
||||
Buffer.BlockCopy(pixels, (cropY + r) * stride + cropX * 4, outPixels, r * dstStride, dstStride);
|
||||
Buffer.BlockCopy(pixels, r * stride, outPixels, r * dstStride, dstStride);
|
||||
}
|
||||
|
||||
var wb = new WriteableBitmap(cropW, cropH, 96, 96, PixelFormats.Bgra32, null);
|
||||
|
||||
Reference in New Issue
Block a user