Originally Posted by
Coolmo
Can someone please show or tell me how to show a drawing file thumbnail image in a picture box using VB.NET? I've been searching the internet for about 6 MONTHS now on how to do this and every snippet of code and "how to" results in errors. I do not want to use Drawingthumbnail.OCX. I have a listbox filled with drawing names that returns the full path of the file I want to show when clicked. It cannot be this hard?! Can it? If someone has this code in one of their programs can you please post it? I'm going crazy here! Thanks in advance.
I'm running AutoCAD 2007 BTW.
Hi Coolmo
Try this code snippet (don't remebere where I'd stoled this one)
Code:
public partial class Form1 : Form
{
string folder = String.Empty;
string dwgname = String.Empty;
string blockname = String.Empty;
public Form1()
{
InitializeComponent();
}
[DllImport("acdb17.dll", EntryPoint = "?acdbDisplayPreviewFromDwg@@YA_NPB_WPAXPBK@Z",
CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
extern static private bool DisplayPreviewFromDwg(string filename, IntPtr hwnd, ref int color);
private void Form1_Load(object sender, EventArgs e)
{
// First create a FolderBrowserDialog object
FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
// Then use the following code to create the Dialog window
// Change the SelectedPath property to the default location
using (FolderBrowserDialog1)
{
// Desktop is the root folder in the dialog.
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.DesktopDirectory;
// Select the C:\Custom directory on entry.
FolderBrowserDialog1.SelectedPath = @"C:\Custom";//<-- initial folder
// Prompt the user with a custom message.
FolderBrowserDialog1.Description = "* Select A Block Library Folder *";
if (FolderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
// Display the selected folder if the user clicked on the OK button.
folder = FolderBrowserDialog1.SelectedPath;
if (!folder.EndsWith("\\", StringComparison.CurrentCultureIgnoreCase))
{
folder = folder + "\\";
}
//MessageBox.Show(folder);//debug only
}
}
string[] fileNames = Directory.GetFiles(folder, "*.dwg");
foreach (string fileName in fileNames)
{
if (fileName.EndsWith(".dwg", StringComparison.CurrentCultureIgnoreCase))
{
FileInfo fi = new FileInfo(fileName);
dwgname = fi.Name;
if (dwgname.Contains(".dwg"))
{
blockname = dwgname.Substring(0, dwgname.LastIndexOf(".dwg"));
this.lstDrawings.Items.Add(blockname.ToUpper());//listBox control
}
}
}
this.Text = blockname;
this.Activate();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void lstDrawings_Click(object sender, EventArgs e)
{
blockname = this.lstDrawings.SelectedItem.ToString();
string dwgname = folder + blockname + ".dwg";
if (File.Exists(dwgname))
{
this.dwgPickBox.Refresh();//PictureBox control
int intColor = System.Drawing.ColorTranslator.ToWin32(Color.White);
bool res = DisplayPreviewFromDwg(dwgname, this.dwgPickBox.Handle, ref intColor);
}
else
{
MessageBox.Show(string.Format("Can't access to file: \n {0}", dwgname));
}
}
}
Working good on my end (A2009)
~'J'~