My question: Looking at the code reference below, how do I change (stream, System.Drawing.Imaging.ImageFormat.Png); to something that would have a direct reference to an image within a file? What are my alternative approaches?
Code sample:
public static byte[] ImageToByte2(Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
Please be more specific. What do you mean by "have a direct reference
to an image within a file"? There's nothing else in your message that
even mentions a file, nor does it make much sense to talk about an array
of bytes having a "direct reference" to anything, never mind an "image
within a file".
As for the code you posted, if you want a way to compress and save
images as bytes in memory, the code you showed should do that just fine.
The one true mistake in the code is your initialization of the
"byteArray" variable; you should not need to initialize it at all, but
if for some reason you need to, assigning "null" makes a lot more sense
than creating a whole new object that is just going to get thrown away.
Beyond that, the code could be a lot more concise. Here's a version
that does the exact same thing:
public static byte[] ImageToByte2(Image img)
{
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return stream.ToArray();
}
}
Note that the MemoryStream does not need to be closed/disposed before
you get the array, nor does the array need to stored in a local variable
at all before being returned.
Pete
I'm learning arrays.
I have created arrays using strings and integers.
I am now looking to create an array to store images.
I have a small .jpg image stored on my desktop, and I want to create a new array using C#, .NET, and store that image within new array.
It appears to me that MemoryStream does that, although it seems to take the image from memory, and not from a file.
As far as the actual reply goes…
On 7/9/11 11:29 AM, Ryan wrote:
> I'm a little stunned by your response.
There is no reason for that.
> It assumes I did not say i was learning C# as an entry level developer.
My response makes no such assumption.
That said, you did NOT in fact make any such statement in your question.
All you wrote was "I'm learning C#", making no reference to your
general experience level.
But it doesn't really matter, as I simply am trying to help you
construct a question that can be answered. There's nothing in my reply
that should have "stunned" you, nor does being an "entry level
developer" mean that you can get away with asking vague questions and
still get useful answers to them.
If you don't like it when people reply to your question with more
questions intended to get you to clarify your original question, then
you might want to rethink the whole "asking questions" thing. Or just
post a better question to start with.
> That being said, I will attempt to answer your questions to the best of my limited 24 hours of experience.
>
> I'm learning arrays.
> I have created arrays using strings and integers.
> I am now looking to create an array to store images.
Why? For what purpose?
> I have a small .jpg image stored on my desktop, and I want to create a new array using C#, .NET, and store that image within new array.
Why? What is the reason for having the image stored in an array? What
are you going to do with the array once the image is stored in it?
I ask, because as bytes in an array, data representing an image isn't
immediately useful. If you have an Image object, you can do things with
it as an image. If you have an array of bytes, all you can do is move
the bytes around, until you use them to create a new Image object. But
if you wanted a new Image object, it's hard to understand why you didn't
just use the original on you had.
> It appears to me that MemoryStream does that, although it seems to take the image from memory, and not from a file.
What do you mean by "take the image from memory"? Given that the image
is in memory already (the Image object, like every other .NET object, is
stored in memory), from where else _would_ the MemoryStream "take the
image"?
If you want to read data from a file and put it into an array, you can
do that. But in that case it doesn't matter _what_ the data is. You
can always just read raw bytes from a file and store those directly into
a byte array, without any regard for what the bytes represent.
Again, since no files are ever mentioned in your post, it's not clear at
all from which file you thought the MemoryStream might have taken the
image data.
Pete
Aside from learning C#. I would like to develop an application that can capture images, using some sort of area screen capture function, and store them in an array or some other data store for further processing. With consideration that there would be thousands of images, I'm not sure what the best approach would be to store the data. I'm open to any suggestions.
My first suggestion (which I mentioned previously already) is that you
figure out how to post to the newsgroup without starting a whole new
thread every time you post.
I don't know the specifics with respect to Google Groups, but from
others I have the impression that they have a "new" and a "classic"
interface, and that you want to use the "classic" interface.
Of course, any standard newsreader would do it correctly by default. At
least half the problem is that you are using Google Groups in the first
place, implemented by people who don't give a crap about Usenet.
Once you have the basics of using Usenet figured out, then we can resume
the actual discussion at hand.
Take a look at the "FromFile" method of Image. I think it will do what
you are looking for.
As far as creating the array, it would be something akin to either:
Image[] images = new Image[someSize]; // where somesize is an
integer.
images[0] = Image.FromFile("filename");
Does that answer your question, or did I miss something?
Matt
Perfectly, thank you.