【问题标题】:Howto convert CefSharp 3 ZoomLevel to percentage and back again如何将 CefSharp 3 缩放级别转换为百分比并再次转换
【发布时间】:2021-02-06 12:47:07
【问题描述】:

CefSharp 浏览器方法:SetZoomLevel 和 GetZoomLevelAsync 使用人类难以理解的 ZoomLevel:https://www.magpcss.org/ceforum/viewtopic.php?f=6&t=11491#p21763

示例值:

150% = Browser.SetZoomLevel(2.2239010857415451);
125% = Browser.SetZoomLevel(1.2239010857415449);
 75% = Browser.SetZoomLevel(-1.5778829311823859);
  1. 有没有一种简单的方法可以通过给定 % 计算 ZoomLevel?
  2. 是否有一种简单的方法可以计算 ZoomLevel 的百分比以向用户显示当前浏览器窗口的百分比?

【问题讨论】:

  • 您的问题是什么?
  • 我已经编辑了这个问题,现在可能更清楚了。

标签: c# browser cefsharp chromium-embedded


【解决方案1】:
public static class CefExtensions
{

    /// <summary>
    /// Converts the given percentage to the ZoomLevel to looke like chrome at zooming in/out
    /// 100 (%) = No Zoom / Reset
    /// </summary>
    /// <param name="pPercentage">Default= 100 = reset zoom</param>
    public static void SetZoomPercent(this ChromiumWebBrowser pBrowser, int pPercentage = 100)
    {
        var lTmp = pPercentage / 100f;
        var lZoomLevel = Math.Log(lTmp, 1.2);
        pBrowser.SetZoomLevel(lZoomLevel);
    }

    /// <summary>
    /// Translates the cef ZoomLevel to the Zoom showed at the Chrome Browser
    /// Usage: Browser.GetZoomPercentAsync().Result
    ///</summary>
    public static async Task<double> GetZoomPercentAsync(this ChromiumWebBrowser pBrowser)
    {
        var lZoomLevel = await pBrowser.GetZoomLevelAsync();
        var lPercentage = Math.Pow(1.2, lZoomLevel) * 100;
        return lPercentage;

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多