C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,482 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,765 people online right now. Registration is fast and FREE... Join Now!




Writing/Reading BMP to text

 

Writing/Reading BMP to text

krszc6

4 Jul, 2009 - 06:08 AM
Post #1

New D.I.C Head
*

Joined: 4 Jul, 2009
Posts: 2

Hello
Need a little help, I'm not advenced programmer.
What i need to do is write and read bmp data to text files without getting inside into bmp file format structrue.

First function of program is reading bmp file through openfile dialog and writing pixel data as greyscale values into tex files.

As You can see its writing greyscale "k" value to text file for each pixel separated by " ".
Now i need to read pixel data and create bmp file in memory and write it with changed extension.
Tried to do it with binary/text reading but no efefcts. Now i have concept to read whole file data as string and create int array from it.


CODE


for (i = 0; i < openFileDialog1.FileNames.Length; i++)
                        {
                          
                            progressBar1.Value++;
                            this.Refresh();
                            string newFile;
                          
                            //newFile = openFileDialog1.FileNames[i].ToString();
                            System.Drawing.Bitmap image = new System.Drawing.Bitmap(512, 512);
                            
                            newFile = Path.GetDirectoryName(openFileDialog1.FileNames[i]) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(openFileDialog1.FileNames[i]) + ".bmp";

                            FileStream br;
                            br = new FileStream(openFileDialog1.FileNames[i], FileMode.Open);
                          
                      string  PixelData = ReadToEnd();
      
                     /* Something to Parse string PixelData into arary

                                    List<int> numbers = new List<int>();
             Numbers.Add( new List<int>( Array.ConvertAll<string, int>( PixelData.Split( ' ' ), new Converter<string, int>( int.Parse ) )  ) );
this dont work
*/
                      

                                   for (int x = 0; x < image.Width; ++x)
                                        {
                                        
                                            for (int y = 0; y < image.Height; ++y)
                                            {
                                              
                                            // set k  value from arary[]
                                              
                                                image.SetPixel(x, y, Color.FromArgb(255,k,k,k));
                                            }
                                    
                                
                            }
                                  
                                   image.Save(newFile);
                                   br.Close();
                        }


Dunno how to change string into array of integers.

All bmp files that im working with are same size 512x512 so always there will be 262144 Integers separated with " ".


Thanks for Your time in Advance.
All the best krs.

This post has been edited by krszc6: 4 Jul, 2009 - 01:59 PM

User is offlineProfile CardPM
+Quote Post


jack.williams

RE: Writing/Reading BMP To Text

4 Jul, 2009 - 09:41 AM
Post #2

New D.I.C Head
*

Joined: 4 Jul, 2009
Posts: 3

string str = "89783 4589798 5463";//whatever your string is
string[] strarray = str.Split(' ');
int[] intarray = new int[262144];
for (int i = 0; i < strarray.Length; i++)
{
intarray[i] = Convert.ToInt32(strarray[i]);
}
User is offlineProfile CardPM
+Quote Post

krszc6

RE: Writing/Reading BMP To Text

4 Jul, 2009 - 01:58 PM
Post #3

New D.I.C Head
*

Joined: 4 Jul, 2009
Posts: 2

Thank you :-) Your code was really helpfull, but finally i used this:
CODE

string buffer = reader.ReadToEnd();
            string[] data = buffer.Trim().Split( ' ' );

            int[] intarray = data.Select( s => int.Parse(s) ).ToArray()


User is offlineProfile CardPM
+Quote Post

ragingben

RE: Writing/Reading BMP To Text

6 Jul, 2009 - 07:34 AM
Post #4

D.I.C Head
Group Icon

Joined: 7 Oct, 2008
Posts: 153



Thanked: 9 times
Dream Kudos: 150
My Contributions
Hi, you may find this link useful - http://www.dreamincode.net/forums/index.ph...c=78002&hl=
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:03AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month