Example #1
0
        // Download an image of an item from Bricklink. Store it in image cache for future use and return it for use by caller directly
        private Image DownloadImageFromBricklink(string itemId, int color, bool useFallackImage = false)
        {
            Image image = null;

            try
            {
                // Path to local cache is stored in the App data folder, under cache. One dir for each color, one file for each image
                var imageCachePath     = ImageCachePathFull(itemId, color);
                var imageCacheDiretory = Path.GetDirectoryName(imageCachePath);

                // No. Make sure cache dir exist first. Will create both root cache dir and color dir as needed
                if (!Directory.Exists(imageCacheDiretory))
                {
                    Directory.CreateDirectory(imageCacheDiretory);
                }

                // Download image from Bricklink
                using (WebClient wc = new WebClient())
                {
                    // Get image as stream of bytes; if fallback we use an image withouth color mapping
                    var    url  = useFallackImage ? BricklinkItems.BricklinkItemImageUrlNoColor(itemId) : BricklinkItems.BricklinkItemImageUrl(itemId, color);
                    byte[] data = wc.DownloadData(url);

                    // Store to bitmap locally
                    using (MemoryStream ms = new MemoryStream(data))
                    {
                        image = new Bitmap(ms);

                        // Save to local cache for future use, if enabled
                        image.Save(imageCachePath);
                    }
                }
            }
            catch // regardless of error
            {
                // Are we using fallback image?
                if (!useFallackImage)
                {
                    // No, so retry to get image but with alternate image url (ie fallback is true). Whatever that may result in, use it
                    return(DownloadImageFromBricklink(itemId, color, true));
                }
                else
                {
                    // Yes - Fallback failed, we end up here only if fallback is used and exception is raised while doing so
                    return(null);
                }
            }

            // Image found (downloaded or cached)
            return(image);
        }
Example #2
0
        // Event: a cell in the table is being formatted. Do some adjustments and add tool tips along the way
        private void dataGridViewItems_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // Item type will show a description as tooltip
            if (e.ColumnIndex == ItemPropertyToDatagridColumnIndex(InventoryItem.ItemProperty.ITEMTYPE))
            {
                var itemTypeDescription = BricklinkItems.BricklinkItemTypeDescription((string)e.Value);
                dataGridViewItems[e.ColumnIndex, e.RowIndex].ToolTipText = itemTypeDescription;
            }
            // Paint color column cells and show tooltip with color description
            else if (e.ColumnIndex == ItemPropertyToDatagridColumnIndex(InventoryItem.ItemProperty.COLOR))
            {
                // Colorize cell
                var color = (int)e.Value;
                var cInfo = BricklinkColors.GetInfo(color);
                e.CellStyle.BackColor = BricklinkColors.FromString(cInfo.Bg);
                e.CellStyle.ForeColor = BricklinkColors.FromString(cInfo.Fg);

                // Also add a color description as tool tip for the cell
                dataGridViewItems[e.ColumnIndex, e.RowIndex].ToolTipText = cInfo.Name;
            }
            // Add a tool tip to item id's with URL that this cell will launch upon click
            else if (e.ColumnIndex == ItemPropertyToDatagridColumnIndex(InventoryItem.ItemProperty.ITEMID))
            {
                var itemId = (string)e.Value;
                var color  = (int)dataGridViewItems[ItemPropertyToDatagridColumnIndex(InventoryItem.ItemProperty.COLOR), e.RowIndex].Value;
                var url    = string.Format(BricklinkCatalogItemLink, itemId, color);
                dataGridViewItems[e.ColumnIndex, e.RowIndex].ToolTipText = url;
            }
            // All other cells, general instruction as tool tip
            else if (e.ColumnIndex == dataGridViewItems.Columns["Image"].Index)
            {
                dataGridViewItems[e.ColumnIndex, e.RowIndex].ToolTipText = dataGridViewItems[e.ColumnIndex, e.RowIndex].Value == null
                    ? "Click button to download missing images (if they exist)"
                    : "Click image to zoom then click anywhere to close zoom";
            }
            else
            {
                var description = dataGridViewItems[e.ColumnIndex, e.RowIndex].ReadOnly
                    ? dataGridViewItems.Columns[e.ColumnIndex].Name
                    : "Click to edit cell\nRight click to edit column";
                dataGridViewItems[e.ColumnIndex, e.RowIndex].ToolTipText = description;
            }
        }