Solved

How to implement over 1000 images into C# application.

Posted on 2004-08-03
4
354 Views
Last Modified: 2010-04-15
Hi
I have to make an application that is some kind of catalog, and the main feature of the application is to showing bunch of product’s images on Windows Forms.
I just wonder how I should implement this feature, I mean what is the best way to store images. Should I use resource file in order to store these, images, or there is better way to access images in C#. I want also prevent users from changing these images.
Since there are over 1000 images (each picture is about 50 –150KB), it is impassible to use embedded resources.

I have problem with Windows Forms as well. The application will require over 80 Windows Forms. Since I can’t use inheritance at least not for each Form, is there good practice to use dll files for these Forms, and call it form main form?

I will appreciate any ideas!!
Thanks
0
Comment
Question by:knobloch
4 Comments
 
LVL 9

Expert Comment

by:s_sansanwal
ID: 11712006
Best is keep the files on the hard drive and display images using picturebox control at run time.
You could have pictures names in sequence and display the images based on the number
e.g->
this.pictureBox1.Image = System.Drawing.Image.FromFile(@"C:\1.jpg");

This is easiest extensible plus your application don't need to save the images in itself.

Cheers,
S Sansanwal
0
 

Author Comment

by:knobloch
ID: 11712191
In this case users could edit or delete images easily. I'm not crazy about application security, but people sometime do things that they are not supposed to do.

0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 125 total points
ID: 11713164
If you have such a complex application, I would suggest to think in two directions: performance and ease of maintanance/future development. So I would make it like this:

- Put the image files in resource dlls, however split them logically in several dlls (1000 files, each about 150 Kb is 150 Mb and if it is in one file, it will be slow and difficult to manage). The users can still change the pictures if they have enough experience/knowledge to work with resource files. But hey, even if you encrypt them, it is still possible to change them, it will only be harder. So it is important how much you want to secure the images. I think putting them in a resource is good

- Put the forms also in DLLs. Split them as well logically, so that you end up with small dlls that are easier to manage (and probably faster to load). Still have in mind inheritance. Maybe you will have several base forms -> put them in a base dll which you will reference from the others. The problem with dlls is that you reach a level, where you need to reference dll 1 from dll 2 and dll 2 from dll 1, which is not possible. So spend some time to plan the dlls, but don't put everything in a huge 100 Mb exe...
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 11716432

Okay... there are a ton of things you can do here.  It all really depends on what you *really* want/need.

Personally, I would put all of the pictures into a single directory and have security lock it down so none of the users have access to that directory.  I would then Place all of my descriptions to those pictures into the advanced properties of the file.  That way the image and the text you want to display are all in one place.


here are some other options as well:

Do you just want to show a list of pictures and their descriptions?

One solution might be to use a panel control, and then dynamically add picture boxes for each of your pictures.

Here's some sample code (dynamic pictureboxes loaded into a scrolling panel, with dynamic labels).


<button click event>
         PictureBox[] p = new PictureBox[7];
         Label[] l = new Label[7];
         panel2.AutoScroll = true;

         for(int i=0;i<6;i++)
         {

            p[i] = new PictureBox();
            p[i].Image = Image.FromFile(@"C:\temp\test" + (i+1) + ".gif");
            p[i].Location = new Point(i*p[i].Image.Width+10,50);
            p[i].Size = new Size(p[i].Image.Width,p[i].Image.Height);


            l[i] = new Label();
            l[i].Text= "test" + (i+1) + ".gif";
            l[i].Location = new Point(i*p[i].Image.Width+10,10);
            l[i].Size = new Size(p[i].Image.Width,15);

            panel2.Controls.Add(l[i]);
            panel2.Controls.Add(p[i]);
         }



As for the pictures themselves.  I'd only reccomend a a ResX file *ONLY* if the pictures were not going to change very often.  Otherwise you need to recreate the resex file every time.  I suppose you could do it in code, but that would take work.  Here is some code I use for creating ResX files.  Very quick and simple.. the form itself has two buttons and 1 label.

using System;
using System.IO;
using System.Resources;
using System.Windows.Forms;

namespace JSI.TAD.CreateResXFiles
{      
      public class ResX
      {
            private static string[] sourceFiles;
            private static FileInfo[] files;

            /// <summary>
            /// Uses Open File Dialog to get files
            /// </summary>
            public static void GetSourceFiles()
            {
                  // User file navigation
                  OpenFileDialog fd = new OpenFileDialog();
                  fd.Multiselect = true;

                  // retrieve files
                  if(fd.ShowDialog() == DialogResult.OK)
                  {
                        sourceFiles = fd.FileNames;

                        // Save File Information
                        files = new FileInfo[sourceFiles.Length];
                        for(int i=0;i<files.Length;i++)
                              files[i] = new FileInfo(sourceFiles[i]);
                  }
                  fd.Dispose();
            }

            /// <summary>
            /// Uses SaveFileDialog to create resx file
            /// </summary>
            public static void WriteSourceFiles()
            {
                  if(files!=null && files.Length>0)
                  {
                        // Create SaveFileDialog
                        SaveFileDialog sd = new SaveFileDialog();
                        sd.Filter = "ResX Files (*.resx)|*.resx|Resource Files (*.resource)|*.resource";
                        sd.FileName = "resFile1";
                  
                        if(sd.ShowDialog()==DialogResult.OK)
                        {
                              Stream myStream;

                              // Generate File if filename given
                              if((myStream = sd.OpenFile()) != null)
                              {
                                    ResXResourceWriter rw = new ResXResourceWriter(myStream);
                              
                                    FileStream str;
                                    byte[] buff;

                                    // Load files into res file
                                    for(int i=0;i<files.Length;i++)
                                    {
                                          str = new FileStream(files[i].FullName,FileMode.Open);
                                          buff = new byte[(int)str.Length];
                                    
                                          str.Read(buff,0,buff.Length);
                                          str.Close();

                                          rw.AddResource(files[i].Name, buff);
                                    }
                                    myStream.Flush();
                              
                                    // generates res file
                                    rw.Generate();
                                    rw.Close();
                              }
                        
                        }
                        sd.Dispose();
                  }
            }
      }
}
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question