c#设置图片的dpi_C#:设计DPI的高分辨率下的显示问题

一、背景

在PC机上显示正常,在高分辨率下的Pad上,显示出现问题:

1、显示在屏幕最右端的窗体(控件)显示不出来;

2、截图时,被截图的界面字体文字变大,界面因此显示不全。

二、解决方法:

方法一:WPF上使用WPF方式获取屏幕大小,而不是Winform的获取屏幕大小的方式。

//Size primarySize = Screen.PrimaryScreen.Bounds.Size;

double dWidth =System.Windows.SystemParameters.PrimaryScreenWidth;double dHeight = System.Windows.SystemParameters.PrimaryScreenHeight;

View Code

方法二:Winform解决方法:

1、设置窗体的背景图片方式改为可缩放方式(Zoom): BackgroundImageLayout = ImageLayout.Zoom;

2、依据DPI扩展拷贝图片的大小,设置拷贝的图片的DPI(bmp的SetResolution方法)

BackgroundImage =GetDestopImage();

BackgroundImageLayout=ImageLayout.Zoom;privateImage GetDestopImage()

{float rate = dpi / 96;

Rectangle rect= Screen.GetBounds(this);

Size sz= newSystem.Drawing.Size();

sz.Width= (int)(rect.Size.Width *rate);

sz.Height= (int)(rect.Size.Height *rate);

Bitmap bmp= newBitmap(

sz.Width, sz.Height, PixelFormat.Format32bppArgb);

bmp.SetResolution(dpi, dpi);

Graphics g=Graphics.FromImage(bmp);

g.CopyFromScreen(0, 0, 0, 0, sz);//IntPtr gHdc = g.GetHdc();//IntPtr deskHandle = NativeMethods.GetDesktopWindow();//IntPtr dHdc = NativeMethods.GetDC(deskHandle);//NativeMethods.BitBlt(//gHdc,//0,//0,//Width ,//Height,//dHdc,//0,//0,//NativeMethods.TernaryRasterOperations.SRCCOPY);//NativeMethods.ReleaseDC(deskHandle, dHdc);//g.ReleaseHdc(gHdc);//bmp.Save("test.png");

returnbmp;

}

View Code

3、修改拷贝内容位置信息

private voidDrawLastImage()

{float rate = dpi / 96;int reWidth = (int)(Width *rate);int reHeight = (int)(Height *rate);using (Bitmap allBmp = newBitmap(

reWidth, reHeight, PixelFormat.Format32bppArgb))

{

allBmp.SetResolution(dpi,dpi);using (Graphics allGraphics =Graphics.FromImage(allBmp))

{

allGraphics.InterpolationMode=InterpolationMode.HighQualityBicubic;

allGraphics.SmoothingMode=SmoothingMode.AntiAlias;

allGraphics.DrawImage(

BackgroundImage,

Point.Empty);

DrawOperate(allGraphics);

allGraphics.Flush();

Rectangle reSelectImageRect= newRectangle();

reSelectImageRect.X= (int)(SelectImageRect.X *rate);

reSelectImageRect.Y= (int)(SelectImageRect.Y *rate);

reSelectImageRect.Width= (int)(SelectImageRect.Width *rate);

reSelectImageRect.Height= (int)(SelectImageRect.Height *rate);

Bitmap bmp= newBitmap(

reSelectImageRect.Width,

reSelectImageRect.Height,

PixelFormat.Format32bppArgb);

bmp.SetResolution(dpi, dpi);

Graphics g=Graphics.FromImage(bmp);

g.DrawImage(

allBmp,0,0,

reSelectImageRect,

GraphicsUnit.Pixel);

g.Flush();

g.Dispose();

_image=bmp;

}

}

}

View Code

4、获取DPI代码:

public static floatgetLogPiex()

{float returnValue = 96;try{

RegistryKey key=Registry.CurrentUser;

RegistryKey pixeKey= key.OpenSubKey("Control Panel\\Desktop");if (pixeKey != null)

{var pixels = pixeKey.GetValue("LogPixels");if (pixels != null)

{

returnValue= float.Parse(pixels.ToString());

}

pixeKey.Close();

}else{

pixeKey= key.OpenSubKey("Control Panel\\Desktop\\WindowMetrics");if (pixeKey != null)

{var pixels = pixeKey.GetValue("AppliedDPI");if (pixels != null)

{

returnValue= float.Parse(pixels.ToString());

}

pixeKey.Close();

}

}

}catch(Exception ex)

{

returnValue= 96;

}returnreturnValue;

}

View Code

原文:http://www.cnblogs.com/shenchao/p/5594831.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值