This topic and its replies were posted before the current version of LEADTOOLS was released and may no longer be applicable.
#1
Posted
:
Monday, June 17, 2013 10:17:27 AM(UTC)
Groups: Registered
Posts: 11
So, seems simple, but I couldn't find an answer on my own. Does the library provide a way of obtaining the correct extension given a format? I could of course write my own (very large...) conditional to get it, but I'd rather not if something already exists.
#2
Posted
:
Tuesday, June 18, 2013 5:19:06 AM(UTC)
Groups: Registered
Posts: 256
Ed,
For the main list of types, which is similar to what our Save dialog displays, you can obtain the list of extension strings such as "tif", "png" and "bmp" using this code:
using Leadtools.WinForms.CommonDialogs.File;
...
RasterSaveDialogFileFormatsList formatsList = new
RasterSaveDialogFileFormatsList(RasterDialogFileFormatDataContent.Default);
for(int formatIndex = 0; formatIndex<formatsList.Count; ++formatIndex)
Console.WriteLine("Index: " + formatIndex.ToString() +
", Format name: " + formatsList[formatIndex].Format.ToString() +
", extension: " + formatsList[formatIndex].Extension);
#3
Posted
:
Tuesday, June 18, 2013 12:39:47 PM(UTC)
Groups: Registered
Posts: 11
Thanks for the response. It's a bit of a round-about method, but it works:
private string GetExtension(RasterImageFormat fmt)
{
var fmtList = new RasterSaveDialogFileFormatsList(RasterDialogFileFormatDataContent.Default);
for (int i = 0; i < fmtList.Count; ++i)
{
if (fmtList[i].Format == fmt)
{
return fmtList[i].Extension;
}
}
return string.Empty;
}
#4
Posted
:
Tuesday, June 18, 2013 1:12:47 PM(UTC)
Groups: Registered
Posts: 11
Sorry, spoke too soon; it doesn't work. It only works for a subset of the supported types. For example, RasterImageFormat.TifLzw (what we happen to use) is not present. I realize the extension is still the same, but this means there is no way to go from a specific sub-type to an extension, which is what I am after.
#5
Posted
:
Tuesday, June 18, 2013 1:14:44 PM(UTC)
Groups: Registered
Posts: 11
Oh, even worse; the Extension property is a filter, suitable for a save dialog, but not for using as an actual extension. As I mentioned in my original post, I need a conversion from RasterImageFormat -> file extension string.
#6
Posted
:
Wednesday, June 19, 2013 5:42:34 AM(UTC)
Groups: Registered
Posts: 256
You're right. The initial code only covers the main file types, which are more like families of image formats. For example, all TIFF sub-types such as uncompressed TIFF, LZW TIFF and JTIF are grouped under one main format.
To get the extensions for the sub-types, you can use the following function:
string GetFilenameExt(RasterImageFormat fileFormat)
{
RasterSaveDialogFileFormatsList formatsList = new
RasterSaveDialogFileFormatsList(RasterDialogFileFormatDataContent.Default);
for (int formatIndex = 0; formatIndex < formatsList.Count; ++formatIndex)
{
RasterSaveDialogFileFormat mainFormat = formatsList[formatIndex];
for (int bppIndex = 0; bppIndex < mainFormat.BitsPerPixelList.Count; ++bppIndex)
{
RasterSaveDialogBitsPerPixel bpp = mainFormat.BitsPerPixelList[bppIndex];
if (bpp.SubFormatsList.Count == 0)
{
if (fileFormat == mainFormat.Format)
return mainFormat.Extension;
}
else
for (int subFormatIndex = 0; subFormatIndex < bpp.SubFormatsList.Count; ++subFormatIndex)
{
RasterSaveDialogFileSubType subFormat = bpp.SubFormatsList[subFormatIndex];
if (fileFormat == subFormat.Format)
return subFormat.Extension;
}
}
}
return ""; //format not found
}
Note:
The empty string "" will usually be returned in 2 cases:
1) If you pass a format type that we don't support writing, such as some Digital Camera RAW formats.
2) If you pass a value not defined in the RasterImageFormat enumeration.
#7
Posted
:
Wednesday, June 19, 2013 8:37:30 AM(UTC)
Groups: Registered
Posts: 11
Ahh, ok; I didn't look closely enough at the BitsPerPixelList property. Thanks for the help, that will work great.
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.