亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 DPI 為 125% 的多臺顯示器修復 CopyFromScreen

如何使用 DPI 為 125% 的多臺顯示器修復 CopyFromScreen

C#
千萬里不及你 2023-05-14 16:38:06
我寫了一個函數,它將接受一個控件和一個文件目標,并將保存控件覆蓋的表單區域。我的問題是,當我從外接顯示器移到筆記本電腦的主屏幕時,捕獲區域移動的量不一致。我終于弄明白縮放比例 (DPI) 是罪魁禍首。當我將它降低到 100% (96 DPI) 時,它在筆記本電腦屏幕上工作。所有其他屏幕都已設置為 100%?;氐?125%,這只是筆記本電腦屏幕上的問題。我如何允許 125%?在筆記本電腦屏幕上,表格越靠近屏幕左上角,圖像的位置就越準確。生成的圖像大小在任何屏幕上都是相同的,只是在筆記本電腦屏幕上時位置會發生變化。此外,當我從外接顯示器過渡到筆記本電腦顯示屏時,表單會調整大小。在此調整大小后,我遇到了這個問題。    private void capture(Control ctrl, string fileName)    {        Rectangle bounds = ctrl.Bounds;        Point pt = ctrl.PointToScreen(bounds.Location);        Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);        using (Graphics g = Graphics.FromImage(bitmap))        {            g.CopyFromScreen(new Point(pt.X - ctrl.Location.X, pt.Y - ctrl.Location.Y), Point.Empty, bounds.Size);        }        string filetype = fileName.Substring(fileName.LastIndexOf('.')).ToLower();        switch (filetype)        {            case ".png":                bitmap.Save(fileName, ImageFormat.Png);                break;            case ".jpeg":                bitmap.Save(fileName, ImageFormat.Jpeg);                break;            case ".bmp":                bitmap.Save(fileName, ImageFormat.Bmp);                break;            default:                break;        }    }
查看完整描述

2 回答

?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

我不確定您是如何使用當前代碼復制屏幕的正確部分的。Bounds ()屬性返回一個相對于PARENT控件的矩形,但您要求控件本身(而不是父控件)轉換為屏幕坐標:


獲取或設置控件(包括其非客戶端元素)相對于父控件的大小和位置(以像素為單位)。


我希望看到更多類似的東西:


Rectangle bounds = ctrl.Parent.RectangleToScreen(ctrl.Bounds);       

Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);

using (Graphics g = Graphics.FromImage(bitmap))

{

    g.CopyFromScreen(bounds.Location, new Point(0,0), bounds.Size);

}

不過,我不知道這是否能在不同的縮放模式下正常工作。


查看完整回答
反對 回復 2023-05-14
?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

我找不到修復它的方法。我更改了方法并在表單上使用了 DrawToBitmap,然后從控件位置制作了一個圖像。我必須考慮一個固定的偏移量。我認為這與頂部欄包含在窗體的位圖中而不是包含在窗體中控件的位置有關。


要考慮的一件事是 DrawToBitmap 以相反的堆棧順序繪制。如果您有疊加對象,您可能需要顛倒 DrawToBitmap 的順序。


private void capture(Control ctrl, string fileName)

{

    Bitmap bitmapForm = new Bitmap(this.Width, this.Height);

    this.DrawToBitmap(bitmapForm, new Rectangle(0, 0, this.Width, this.Height));

    Rectangle myControlRect = new Rectangle(ctrl.Location,ctrl.Size);

    //Correct for boarder around form

    myControlRect.Offset(8,31);

    Bitmap bitmap = bitmapForm.Clone(myControlRect, PixelFormat.DontCare);

    string filetype = fileName.Substring(fileName.LastIndexOf('.')).ToLower();

    switch (filetype)

    {

        case ".png":

            bitmap.Save(fileName, ImageFormat.Png);

            break;

        case ".jpeg":

            bitmap.Save(fileName, ImageFormat.Jpeg);

            break;

        case ".bmp":

            bitmap.Save(fileName, ImageFormat.Bmp);

            break;

        default:

            break;

    }

}


查看完整回答
反對 回復 2023-05-14
  • 2 回答
  • 0 關注
  • 169 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號