PurplePen.MapUtil.ValidateMapFile C# (CSharp) Method

ValidateMapFile() public static method

public static ValidateMapFile ( string mapFileName, float &scale, float &dpi, Size &bitmapSize, RectangleF &mapBounds, MapType &mapType, string &errorMessageText ) : bool
mapFileName string
scale float
dpi float
bitmapSize System.Drawing.Size
mapBounds System.Drawing.RectangleF
mapType MapType
errorMessageText string
return bool
        public static bool ValidateMapFile(string mapFileName, out float scale, out float dpi, out Size bitmapSize, out RectangleF mapBounds, out MapType mapType, out string errorMessageText)
        {
            scale = 0; dpi = 0;
            mapType = MapType.None;
            bitmapSize = new Size();
            string fileExtension = Path.GetExtension(mapFileName);

            if (string.Compare(fileExtension, ".pdf", StringComparison.InvariantCultureIgnoreCase) == 0) {
                if (ValidatePdf(mapFileName, out dpi, out bitmapSize, out errorMessageText) != null) {
                    mapType = MapType.PDF;
                    mapBounds = new RectangleF(0, 0, (float)bitmapSize.Width / dpi * 25.4F, (float) bitmapSize.Height / dpi * 25.4F);
                    return true;
                }
                else {
                    mapBounds = new RectangleF();
                    return false;
                }
            }

            Map map = new Map(TextMetricsProvider, new GDIPlus_FileLoader(Path.GetDirectoryName(mapFileName)));

            try {
                InputOutput.ReadFile(mapFileName, map);
            }
            catch (Exception e) {
                // Didn't load as an OCAD file. If it has a non-OCD/OpenMapper extension, try loading as an image.
                if ((string.Compare(fileExtension, ".ocd", StringComparison.InvariantCultureIgnoreCase) != 0) &&
                    (string.Compare(fileExtension, ".omap", StringComparison.InvariantCultureIgnoreCase) != 0) &&
                    (string.Compare(fileExtension, ".xmap", StringComparison.InvariantCultureIgnoreCase) != 0))
                {
                    try {
                        Bitmap bitmap = (Bitmap) Image.FromFile(mapFileName);
                        bitmapSize = bitmap.Size;
                        dpi = bitmap.HorizontalResolution;
                        bitmap.Dispose();
                        mapType = MapType.Bitmap;
                        mapBounds = new RectangleF(0, 0, (float)bitmapSize.Width / dpi * 25.4F, (float)bitmapSize.Height / dpi * 25.4F);
                        errorMessageText = "";
                        return true;
                    }
                    catch {
                        // Wasn't an bitmap file either.
                        errorMessageText = string.Format(MiscText.CannotReadImageFile, mapFileName);
                        mapBounds = new RectangleF();
                        return false;
                    }
                }

                errorMessageText = string.Format(MiscText.CannotReadMap, e.Message);
                mapBounds = new RectangleF();
                return false;
            }

            using (map.Read())
            {
                scale = map.MapScale;
                mapBounds = map.Bounds;
            }

            errorMessageText = "";
            mapType = MapType.OCAD;
            return true;
        }

Usage Example

Example #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                containingWizard.MapFileName = mapFileNameTextBox.Text = openFileDialog.FileName;
                mapFileDisplay.Visible       = true;

                string     errorMessageText;
                float      dpi; // not used here.
                float      mapScale;
                MapType    mapType;
                Size       bitmapSize;
                RectangleF mapBounds;
                if (MapUtil.ValidateMapFile(containingWizard.MapFileName, out mapScale, out dpi, out bitmapSize, out mapBounds, out mapType, out errorMessageText))
                {
                    // map file is OK.
                    containingWizard.MapScale   = mapScale;
                    containingWizard.MapType    = mapType;
                    containingWizard.BitmapSize = bitmapSize;
                    containingWizard.mapBounds  = mapBounds;
                    errorDisplayPanel.Visible   = false;
                    infoDisplayPanel.Visible    = true;
                    ((Control)ParentForm.AcceptButton).Focus();
                }
                else
                {
                    // map file is not OK. Show message.
                    errorMessage.Text         = errorMessageText;
                    infoDisplayPanel.Visible  = false;
                    errorDisplayPanel.Visible = true;
                }
            }
        }
All Usage Examples Of PurplePen.MapUtil::ValidateMapFile