-2

There are already lots of classes and functions supplied with the .NET to manipulate Images including PNG. Like Image, Bitmap, etc. classes. Suppose, I don't want to use those classes.

If I want to manually read/write a PNG image as a binary file to work with pixels then how can I do that?

using(FileStream fr = new FileStream(fileName, FileMode.Open)) 
{
      using (BinaryReader br = new BinaryReader(fr))
      {
          imagesBytes= br.ReadBytes((int)fr.Length);
      }  
}

How can I get hold of individual pixels to manipulate them?

Community
  • 1
  • 1
user366312
  • 16,949
  • 65
  • 235
  • 452
  • 1
    It's all in the specifications. Look here for a start: http://stackoverflow.com/q/26456447/2564301 – Jongware Mar 05 '16 at 14:47

3 Answers3

0

Convert Image to byte[] array:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}

Convert byte[] array to Image:

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

If you want to work with Pixels here's how :

 Bitmap bmp = (Bitmap)Image.FromFile(filename);
                Bitmap newBitmap = new Bitmap(bmp.Width, bmp.Height);

                for (int i = 0; i < bmp.Width; i++)
                {
                    for (int j = 0; j < bmp.Height; j++)
                    {
                        var pixel = bmp.GetPixel(i, j);

                        newBitmap.SetPixel(i, j, Color.Red);
                    }
                }  
  • 1
    No @AtmaneELBOUACHRI, if you want work with pixel, best choice is convert to Bitmap, not image, which is same with asker old intent. – NoName Mar 05 '16 at 13:05
  • Not sure how that answers the question. I already said that I don't want to use `Image` and `Bitmap` classes. – user366312 Mar 05 '16 at 16:33
0

The easiest way is use ReadAllBytes and WriteAllBytes function:

byte[] imageBytes = File.ReadAllBytes("D:\\yourImagePath.jpg");    // Read
File.WriteAllBytes("D:\\yourImagePath.jpg", imageBytes);           // Write
NoName
  • 7,940
  • 13
  • 56
  • 108
0

Although it might be a bit late to bring up this conversation, I wanted to share that I got inspired by this post and wrote my own program in C# to access the pixels of a PNG image. You can check out my repository CrossPlatformSupportOnlyPNG

However, unlike BMP format, pixels in PNG files are compressed with a different algorithm to reduce their size. It took me a few months to go through Wikipedia documentation on this topic, and I must admit that I got overwhelmed with information and gave up for a while.

With .NET Core, I suggest you that to check out some online repositories or libraries to figure out how to access pixels in PNG files. because so many library's were made to store image in server. So decently their will be one which read the pixel array.

so for starter you need to read the open the image as stream.

using (var reader = File.OpenRead(inputFile))
{
  //code goes here.
}

or you can just load the whole image in the memory with

File.ReadAllBytes(inputFile)

then you need to slice them into bytes and convert those slices into human readable data. but what i suggest you first you go through this Link. Understand what is inside the png file. then write your code. As far i have learned through out my journey what you are looking for is IDAT header. but the real problem arrives when you have to decode it into actual pixels. that is because of different algorithms use for compression. so good luck if you continues the png reading and the repo should give you a head start.
or you can check SixLabors.ImageSharp library.

ov poddar
  • 41
  • 6