Click here to Skip to main content
15,665,276 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi!
I am a beginner programmer and I want to create something like this->
I want a program that contains images and their description (user can add image or/and description). Therefore I want to store program’s data somewhere in one file. I wanted to use Resources that would be built if some new data is acquired. The example code is below:



creating resources:
C#
try
{
    ResourceWriter rw = new ResourceWriter("xxx.dll");
    rw.AddResource("o", Resource1.STALMEt);
    rw.Generate();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}


and than restoring:

C#
try
           {
               ResourceManager rm = new ResourceManager("xxx.dll", Assembly.GetExecutingAssembly());
               Image a = (Image)rm.GetObject("o");

           }
           catch (Exception es)
           {
               MessageBox.Show(es.Message);
           }


I get this error:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "xxx.dll.resources" was correctly embedded or linked into assembly "Resorce_TEST" at compile time, or that all the satellite assemblies required are loadable and fully signed.

//Resorce_Test is solution name.
I don`t understand this error and why isn`t this working. I could really use some help.
Thanks a lot for help.
Posted

It looks like you only need static resources. Then you don't need all that.

Create new .RESX resource. Add some stings, image files, etc. Under the project node of your resource, Visual Studio will create auto-generated C# code file. Open it. You will find static declaration of every resource you have added. Use those declarations in you code like regular static properties!

[EDIT]
Answering a follow-up Question.

Look at your auto-generated C# file you provided below. Let's make use of the resource "STALMEt", which is a bitmap. No need to use any code or resource manager. You only need to use static class Resource1 and its static properties. By the way, always use sensible name, rename everything out of default "Resource1", allow no numeral, follow Microsoft naming conventions.
Consider you're writing it in some form:

C#
public partial class MyForm : Form {
    //...

    //that's all about using resource, nothing else:
    static Bitmap bmp = Resorce_TEST.Resource1.STALMEt;    
    //...

    //this is how you can use that bitmap, for example:
    protected override void OnPaint(PaintEventArgs e) {
       base.OnPaint(e);
       e.Graphics.DrawImage(bmp, new Point(10, 12)); //here you use it
    } //OnPaint

    //...
} //class MyForm


That's it. It it clear now?

[EDIT]
See another Answer of persistence for the user data.

—SA
 
Share this answer
 
v4
Comments
Member 7661699 2-Mar-11 11:40am    
I have already created Resoureces.RESX and I believe I understand how it works. I will place some data and load it when necessary in code (example- Image x=resource1.Image). I don’t know however how to store for example image that user added (within program not in code) to the program and use it later. Could You perhaps write some code? I believe I would understand what You mean much quicker :)
Sergey Alexandrovich Kryukov 2-Mar-11 20:07pm    
There is no code. You should not use resource manager directly. All code is auto-generated. Look at the class generated in C# for each resource file, everything is clear. If you still cannot find it, post one such file (please, make it as simple as you can, add one single resource string, that's enough, use "Improve question") and notify me by replying to this comment -- I'll show you.

It should be C#, not .resx file, found in your project as a child node of your .resx node.
--SA
Member 7661699 4-Mar-11 11:14am    
I`m sorry for writing another comment but I believe that You don’t understand my problem or I`m not really bright :D. I don’t know why you are saying that there was no code=> there`s a button user clicks it, openfiledialog shows up and he chooses a jpg file. Than I want the program to store this file to resources or to some file.
I opened c# resource files (if it`s what You mentioned):


namespace Resorce_TEST {
using System;


///
/// A strongly-typed resource class, for looking up localized strings, etc.
///

// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resource1 {

private static global::System.Resources.ResourceManager resourceMan;

private static global::System.Globalization.CultureInfo resourceCulture;

[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resource1() {
}

///
/// Returns the cached ResourceManager instance used by this class.
///

[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Resorce_TEST.Resource1", typeof(Resource1).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}

///
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
///

[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}

internal static System.Drawing.Bitmap STALMEt {
get {
object obj = ResourceManager.GetObject("STALMEt", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
}
}
I still don’t know what to do with it. If its what you said, I'll 'improve question' and delete this comment.

Thanks for Your help :)
Sergey Alexandrovich Kryukov 4-Mar-11 16:29pm    
No problem: I promised to answer.

Please see my update based on your sample. Essential usage sample is just one line, first one inside the declaration of MyForm. It does not matter is this variable is static or not or even exist: you can write Resource1 static variable right at the place of drawing or whatever.

Hope now you can see how to use it.
--SA
Member 7661699 4-Mar-11 17:31pm    
OK :) I know how to use object (image etc.) that has been already added to resources ('drag and dropped' there) and than pictureBox1.Image=Resource1.STALMEt I really do :). But my question was how to add and use object (through resources or anything else)that has not been added (if that`s even possible) through visual.
Perhaps I`m not precise. Idea of program is that this soft can be developed by user. So I as programmer create it and than user (that does not have visual or anything) can add text, picture etc. Program stores it somehow (for example in one file; maybe not even in resources files. like I said I’m not pro). Than user can use this added images, text etc.
I don`t want You to write entire program, I`d do this myself I just don’t know how :)
Answering second follow-up question.

First of all, you should know where to persist user data. This is only one fully legitimate way: you need to use special directory create per user, or create and use sub-directory of a special directory.

You need to use System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) method with the parameter. See other members of the enumeration type Environment.SpecialFolder.LocalApplicationData. Also, see two overloads of the method System.Environment.GetFolderPath.

Now when you have a directory to store your local application data, you may want to create a sub-directory specific to your application to persist the last data chosen by your user.

A second problem is how to persist data. Image files have their own load/store methods, so you can use them. You may need to integrate all your data in one file using some schema. I would highly recommend developing a special set of pure data classes (object graph) and persist it all in one file (most likely, XML). You can keep all image files as separate files, which is convenient but needs some coding effort (unique names, overwriting or keeping old files, etc.). Alternatively, you can embed each image file in your single data file (which is very bulky and not readable; with XML you will need to store binary files in Base64 fragments).

I would hardly recommend using System.Runtime.Serialization.DataContractSerializer. This is most non-intrusive and robust solution: you don't have to make your data serializeable or modify in any way. You only need to add attributes [DataContract] and [DataMember] attributes to your classes and members you want to persist (don't forget to specify you world-unique XML name space in each DataContract attribute. See System.Runtime.Serialization.DataContractAttribute and System.Runtime.Serialization.DataMemberAttribute. The serializer can even persist any object graph which is not a tree (with circular references), which is very robust. It's supportability is mainly based on the fact that if you modify anything in your data classes which is not part of contract, you persisted data will be read. If you migrate your schema incrementally (let's say, by adding new contract members and types but not removing any of the existing ones), your persisted schema will be read anyway. This is like automatic reconciliation of version upgrades. See http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900