|
I want to store images in text form and later load them again.
I got this code to convert and image to a base64 string:
ImageConverter converter = new ImageConverter();
byte[] raw = new byte[1];
Bitmap test = new Bitmap(OFD_Open.FileName);
raw = (byte[])converter.ConvertTo(test, typeof(byte[]));
string output = Convert.ToBase64String(raw);
File.WriteAllText("image.txt", output); And this code to convert it from a string back to an image
byte[] raw = new byte[1];
Bitmap pic = new Bitmap(1, 1);
raw = Convert.FromBase64String(OFD_Open.FileName);
PictureBox box = new PictureBox();
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
pic = (Bitmap)tc.ConvertFrom(raw); But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string.
What went wrong?
Thanks.
|
|
|
|
|
Hi,
the Convert.FromBase64String expect a base64-encoded string, which is in your case stored within a file. But you pass the name of the file. You have to pass the content of the file!
|
|
|
|
|
Megidolaon wrote: raw = Convert.FromBase64String(OFD_Open.FileName);
I think converting the filename won't help much...
If I were you I would convert the content!
From the top of my mind perhaps this will work:
Convert.FromBase64String(File.ReallAllText(OFD_Open.FileName));
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
Hi,
I have no experience with ImageConverter and have never seen any code using it. What most people do is save the image to a MemoryStream, get its byte array, call Convert.ToBase64String; then do the opposite to get it back. The advantage is the code really is very symmetrical, which is great as you want it to behave symmetrically too.
BTW: your initializations with dummy data/sizes don't make sense, just leave them out. I mean the new byte[1] and new Bitmap(1,1)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Wow, that was really stupid.
I totally forgot to read the file content.
Thanks.
It really helps to have someone else look at your code, you sometimes overlook the most basic things.
|
|
|
|
|
I see that there are third party tools and libraries available for accessing Smart Cards through PC/SC but nothing that the .NET framework specifically provides for.
What does this mean? Did Microsoft specifically reject the PC/SC specification or have they just not gotten around to incorporating it into the .NET framework? Does C# (or the .NET framework for that matter) provide any other standard for accessing Smart Cards or is writing a wrapper for the PCSC WIN32 API the way to go? By the way, there's a nice CodeProject article for doing that available here[^].
Basically, what I'd like to know is this. How would Microsoft access Smart Cards when coding in C#?
|
|
|
|
|
how can i add C1.C1Zip namespace in my c# .net application .Actually i want to use the "C1ZStreamReader" class to zip files.
But i am using it first time ....
and getting error message as-
"can't find assembly reference"
plz suggest me how to the the assembly reference.
|
|
|
|
|
Add a reference to the dll in your application.
|
|
|
|
|
Hello,
At present I am able to execute a vbscript on a remote machine.
The way i do this is that the code creates the vbscript file on the server, runs it, returns the result and then deletes the script file from the server.
The above is done by a combination of IO.WriteAlltext and
...
process.start();
Question:
How is it possible to run the script on a remote machine whithout copying the script file on the remote server. i.e. I would like to run the vbscript from the local machine?
but pointing to the remote machine so this way I will not have to write the script file to the remote machine.
Thanks
|
|
|
|
|
I am creating a windows form and have come across this error message, i cannot find information about how to resolve it.
Has anyone seen it before or know what to do.
The variable 'resources' is either undeclared or was never assigned
dont see how it could have happened either as 'resources' is a variable created behind the form to my knowledge
|
|
|
|
|
Apparently the answer is to close down the project and open it again and it works fine.
Vs just wanted to be awkward.
|
|
|
|
|
Sometimes rebuilding helps as well with these weird errors.
I get them a lot when using usercontrols.
|
|
|
|
|
Yeah that did work although there is another error:
Warning 1 Object reference not set to an instance of an object.
which appears every now and again and prevents me seeing the form in design view.
|
|
|
|
|
You don't happen to be using vs2005
I noticed I have it there more than in vs2008 (can't remember having it in 2008 ever actually)
When rebuilding the project its best to have the designer and maybe even the code closed.
Don't know why it happens, all I can think off is that it's a bug.
|
|
|
|
|
Yes it is strange.
will have to try that
Hopefully its a bug with vs and not my code.
Thanx for the help
|
|
|
|
|
is it an error or a warning?
Generally I see those as a warning when I did something like:
int my_int;
instead of
int my_int = 0;
it warns you in order to avoid being null when running a statement later.
hope this helps.
|
|
|
|
|
Hi all,
i can use something like the code below to get all the directory within a given folder.
DirectoryInfo di = new DirectoryInfo(@"C:\");
foreach (DirectoryInfo d in di.GetDirectories("*.*", SearchOption.AllDirectories))
{
Console.WriteLine(d.Name);
}
but as soon as i hit a folder i dont have access to it crashes.
is there anyway i can perform the same action but get it to skip folder i don't have access too?
Many Thanks,
Phil
|
|
|
|
|
|
use try-catch inside the loop. That is what I do.
BTW: .NET 4.0 will offer better methods to enumerate files/folders, without requiring a (possibly huge) array, so one will be able to really use the recursive mode.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
That will not work. The exception will occur while executing the GetDirectories method itself and control will never go inside the try/catch .
|
|
|
|
|
It works for me, I am listing all files/folders of any device I choose (both XP and Vista).
If GetDirectories() and GetFiles() would not cope with inaccessible files and folders, they would be completely useless, wouldn't they?
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
In XP SP2, I created a folder(c:\TestFolder) and "denied" all the permissions under security tab. GetDirectories method itself will throw exception. Control never came to try/catch. See my code:
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\");
foreach (DirectoryInfo dir in dirInfo.GetDirectories("*.*", SearchOption.AllDirectories)) {
try {
}
catch {
}
}
|
|
|
|
|
That is new to me. I'll have to investigate.
And what is your solution?
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Never faced this issue, so far. If I do, I would go for Linq. May be something along the lines of this[^] approach.
If it .Net 2.0, Linq bridge[^] might save me.
|
|
|
|
|
AFAIK there is no such option present. You will have to write your own method to do this.
|
|
|
|