Adding rectangles to a chart (flush with the edges)

Cybert99

New Member
Joined
Mar 11, 2017
Messages
2
On a userform I have a 5x5 grid of image controls. (Each control is 32pts x 32 pts.) The img controls can be different colors.

I can generate a .png from this by creating a worksheet chart (160pts x 160pts) and adding 32x32 rectangles. This works great. My problem is that I can't seem to add my rectangles flush with the left and top edges of the worksheet chart. There is about an 8 pixel gap on the top and left sides, and a 1 pixel gap on the bottom and right sides.

How do I fix this?

If it helps here is my code. (Clearly, I am NOT a programmer.)

Code:
    Dim tmpChart As Chart, n As Long, shCount As Long, sht As Worksheet, sh As Shape
    Set sht = ActiveWorkbook.ActiveSheet
    Set tmpChart = Charts.Add
    tmpChart.ChartArea.Clear
    tmpChart.Name = "MyChart"
    Set tmpChart = tmpChart.Location(Where:=xlLocationAsObject, Name:=sht.Name)
    tmpChart.ChartArea.Width = UserForm1.myColumns * UserForm1.tileWidth
    tmpChart.ChartArea.Height = UserForm1.myRows * UserForm1.tileHeight
    tmpChart.Parent.Border.LineStyle = 0
    tmpChart.ChartArea.Format.Fill.Visible = msoFalse
    tmpChart.ChartArea.RoundedCorners = False
    tmpChart.ChartArea.Select
    
    sNum = 1
    For sCol = 1 To UserForm1.myColumns * UserForm1.tileWidth Step UserForm1.tileWidth
        For sRow = 1 To UserForm1.myRows * UserForm1.tileHeight Step UserForm1.tileHeight
            If UserForm1.Controls("imgTest" & sNum).BackColor <> -2147483633 Then
                Set rect = tmpChart.Shapes.AddShape(msoShapeRectangle, sRow, sCol, UserForm1.tileWidth, UserForm1.tileHeight)
                rect.Fill.ForeColor.RGB = UserForm1.Controls("imgTest" & sNum).BackColor
                rect.Line.Visible = False
            End If
            sNum = sNum + 1
        Next
    Next
    
    tmpChart.ChartArea.Width = userform1.tileWidth * userform1.myRows * PointsPerPixelX
    tmpChart.ChartArea.Height = userform1.tileHeight * userform1.myCols * PointsPerPixelY


    ActiveChart.Export "F:\json\" & InitialFileName & ".png"
    
    'Clean up
    Worksheets("Sheet1").ChartObjects.Delete

NOTE: My ultimate goal is to create a .png image that is exactly 160 pixels wide and 160 pixels tall. (With 5 rows and 5 columns of squares that are 32x32 pixels.) So after I add shapes to the chart I scale it. This doesn't quite work due to the extra blank pixels in the image.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
To be clear, I am able to generate an image by adding shapes to a worksheet chart and then using this command: "ActiveChart.Export"

Here's what my .png images look like: (you can see the empty space on the edges of the image -- I'd rather not have to crop all my images by hand)

506251d1489093536-adding-shapes-to-a-chart-flush-with-the-edges-saved_image.jpg
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,020
Members
449,203
Latest member
tungnmqn90

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top