vba 如何以像素为单位设置图表的宽度和高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14629418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:51:44  来源:igfitidea点击:

How to set width and height of a chart in pixels?

excelvbacharts

提问by peter

Setting width and height of the ChartObjector its Shapedoesnt do it. They both seem to be just some inner part of that white rectangle which is the embedded chart. Also the ChartAreadoesn't seem to be the object I'm interested in.

设置ChartObject或它的宽度和高度Shape不这样做。它们似乎都只是嵌入图表的白色矩形的内部部分。另外,ChartArea似乎并没有成为我感兴趣的对象。

I want the exported file in the following example to have dimensions 800x600 (and I don't mean rescaling the exported image or trial and error until the size accidentally fits). There must be some object around the chart which I have overlooked.

我希望以下示例中的导出文件的尺寸为 800x600(我的意思不是重新缩放导出的图像或反复试验,直到尺寸意外适合)。图表周围一定有一些我忽略的对象。

Sub mwe()

Dim filepath As String
Dim sheet As Worksheet
Dim cObj As ChartObject
Dim c As Chart
Dim cShape As Shape
Dim cArea As chartArea

filepath = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\"))
Set sheet = ActiveSheet
Set cObj = sheet.ChartObjects(1)
Set c = cObj.chart
Set cShape = sheet.Shapes(cObj.Name)
Set cArea = c.chartArea

cObj.Width = 800
cObj.Height = 400
MsgBox cArea.Width & " x " & cArea.Height  '793x393
c.Export filepath & "test1.png"            '1067x534, this is also the size on screen

cShape.Width = 800
cShape.Height = 400
MsgBox cArea.Width & " x " & cArea.Height  '794x393
c.Export filepath & "test2.png"            '1068x534, this is also the size on screen

End Sub

update:

更新:

It turns out the ChartObjectand the Shapebelonging to the same Chartalready have the Worksheetas parent but width and height are not specified in pixels but in points, where 1 point = 1/72 inches, and most of the time, Windows seems to assume 96 pixels per inch.

事实证明ChartObjectShape属于同一个的 和Chart已经有Worksheet父级,但宽度和高度不是以像素为单位指定的,而是以点为单位指定的,其中 1 点 = 1/72 英寸,并且大多数情况下,Windows 似乎假定每个像素为 96 像素英寸。

Thanks to Steve's comment I am now using the following, which is quite reliable.

感谢史蒂夫的评论,我现在正在使用以下内容,这是非常可靠的。

A ChartObjectthat is not activated at the moment of export seems to produce a file where width and height is 1 higher than it should be.

ChartObject导出时未激活的A似乎生成的文件的宽度和高度比应有的高 1。

It remains to find out how to determine the factors px2ptHand px2ptVautomatically.

它仍然找出如何确定的因素px2ptHpx2ptV自动。

Sub mwe()

Dim filepath As String
Dim sheet As Worksheet
Dim cObj As ChartObject
Dim c As Chart

Dim px2ptH As Double: px2ptH = 72 / 96
Dim px2ptV As Double: px2ptV = 72 / 96
Dim w As Double: w = 800 * px2ptH
Dim h As Double: h = 400 * px2ptV

filepath = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\"))
Set sheet = ActiveSheet
Set cObj = sheet.ChartObjects(1)
Set c = cObj.Chart

'otherwise image size may deviate by 1x1
cObj.Activate

cObj.Width = w
cObj.Height = h

c.Export filepath & "test.png"

End Sub

回答by Steve Rindsberg

The dimensions are in points, not pixels. 72 points to the inch.

尺寸以点为单位,而不是像素。72 点为英寸。

800 pixels / 72 = 11.11111.... The size of the exported image will be the size in inches/the dpi of your computer's display, usually 96 (as it is in your case, but you can't always count on it ... use WIN API calls to find the current value)

800 像素 / 72 = 11.11111 .... 导出图像的大小将以英寸/计算机显示器的 dpi 为单位的大小,通常为 96(就像您的情况一样,但您不能总是指望它。 .. 使用 WIN API 调用来查找当前值)

Randy Birch has published code you can use to access the WinAPI calls to get screen resolution and more.

Randy Birch 发布了可用于访问 WinAPI 调用以获得屏幕分辨率等的代码。

Go to http://vbnet.mvps.org/index.htmland use the search feature on the left to look up GETDEVICECAPS

转到http://vbnet.mvps.org/index.html并使用左侧的搜索功能查找 GETDEVICECAPS