This project has moved and is read-only. For the latest updates, please go here.

Images and related Files to KMZ

Oct 3, 2012 at 3:17 PM

Hi,

I am struggling to add images and related files to a KMZ file.

Can you please give an example of how to add Images (which is used for IconStyles ) to the KMZ File, including the existing KML file?

And if possible how to structure such images into a folder structure..

 

Thank You,

Much Appreciated.

Oct 3, 2012 at 4:12 PM

I actually got it figured out. Here is my Method for Saving the KMZ file, including the Image which is used for the Icon.

To Add the directory inside the KMZ, I specified the folder name in front of the file. Obviously this must then be correctly referenced when creating the IconStyle Uri.

     

private const string KmlDir_Images = "Images";
private const string ImageSignal = "IconSignal.png";
private const string ImageFullPath = "C:\\IconSignal.png";

public void SaveKMZFile(Element root)
{
	using (SaveFileDialog d = new SaveFileDialog {Filter = "KMZ File|*.kmz"})
	{
		if (d.ShowDialog() != DialogResult.OK)
			return;

		KmlFile kml = KmlFile.Create(root, false);
		using (KmzFile kmz = KmzFile.Create(kml))
		{
			using(FileStream fs = File.OpenRead(ImageFullPath))
			{
				using(BinaryReader reader = new BinaryReader(fs))
				{
					kmz.AddFile(KmlDir_Images +"/"+ ImageSignal, reader.ReadBytes((int)reader.BaseStream.Length));
					kmz.Save(d.FileName);

					reader.Close();
					fs.Close();
				}
			}
		}
	}
}

Oct 6, 2012 at 11:19 AM

Glad you figured it out and thanks for posting the example here for others to find!