Click here to Skip to main content
12,954,522 members (57,623 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 27 Mar 2010

Showing Image in Frame ActiveX Control

, 27 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes how the image can be shown in Frame ActiveX control with proper size.


This article is related to Office development for Microsoft Excel. Normally for showing an image in Excel file, we can use Forms.Image control, but this control causes some distortion to the image and the image is shown blurred when the control loses focus. For resolving this issue, another control can be used to show the images and that is Forms.Frame control.

But the Frame control has some drawbacks like it does not resize itself according to the size of the actual image, in essence, it does not have auto size feature as is available with Image control. So we need to set the size of frame object through code as per the size of image. But the problem does not end here. Image size measurement unit is different than the measurement unit of Excel.

So here in this article, I present the solution to set the Frame size appropriately. This includes some magic numbers also. Let's have fun with these numbers.


It is easy doing things manually but when it comes to doing things programmatically, things become little bit tricky. I needed to develop an Excel Addin in which I needed to show images on Excel. Initially I added Forms.Image controls to show the images but that was not working as per expectations so I decided to use Forms.Frame control that shows a very sharp image and does not blur when it loses focus.

Using the Code

I am not detailing out here how to create addin but I will discuss only how to add Frame control programmatically and how to size it as per the actual image size.

So here goes the code for adding Forms.Frame control to Excel:

int top = 100, left = 100, width = 100, height = 100;
Excel.OLEObject oleObj = null;		
Excel.OLEObjects oleObjCollection = ((Excel._Worksheet)
	as Excel.OLEObjects;
// This code adds Frame object, with some initial dimensions, 
// to OLE Object collection of Excel.
objObj = oleObjCollection.Add("Forms.Frame.1", Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
left, top, width, height);
Forms.Frame objFrame = oleObj.Object as Forms.Frame;

Here the applicationObject is the Excel Application object from which we first retrieve the OLE Object collection of the workbook and then add the Frame object to that collection to get that frame on Excel.

Once the Frame control is added to Excel, we set the image for that frame and then set the size of that frame. And that is the tricky part, when we calculate the size of image it comes in pixels but to set the size of frame, we need to convert that pixel value into points that is the size measurement unit of Excel.

System.Drawing.Image img = System.Drawing.Image.FromFile("C://image.jpg");
objFrame.Picture = ImageConverter.Convert(img) as stdole.StdPicture;
oleObj.Width = GetPixelToPointWidth(img.Width);
oleObj.Height = GetPixelToPointHeight(img.Height);		

Before setting the picture to Frame object, we need to convert that picture in compatible format and for that, we need to write ImageConverter class, as GetIPictureDispFromPicture method is not available directly because of its protection level.

using System.Windows.Forms;
public class ImageConverter : AxHost 
	private ImageConverter(): base(null) 
	public static IPictureDisp Convert(System.Drawing.Image image) 
		return AxHost.GetIPictureDispFromPicture(image) as IPictureDisp;

And here is the tricky part. These methods convert the pixel value to point value according to the DpiX and DpiY values of Excel. And here comes the magic number, that is 72.

private float GetPixelToPointWidth(int w)
	System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd
	return (w / g.DpiX)* 72;
private float GetPixelToPointHeight(int h)
	System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd
	return (h / g.DpiY)*72;

Points of Interest

This conversion factor can be applied in some other situations also where we need to calculate the height, width for Excel and Ole objects.

You can reach me at for any queries or suggestions regarding this article.


  • 27th March, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionChild controls to Forms.Frame control Pin
Member 333546211-Jun-12 10:39
memberMember 333546211-Jun-12 10:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 27 Mar 2010
Article Copyright 2010 by cool_manu07
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid