0

In my code I load a 32-bit-depth PNG file (1 pixel 4 byte) into a byte array like this:

//Pick a file
OpenFileDialog FileValaszto = new OpenFileDialog();
FileValaszto.ShowDialog();

//Create BitmapImage
BeolvasottKep = new BitmapImage(new Uri(FileValaszto.FileName));

//Create byte array
stride = BeolvasottKep.PixelWidth * 4;
int size = BeolvasottKep.PixelHeight * stride;
MyPixelsArray = new byte[size];
BeolvasottKep.CopyPixels(pixels, stride, 0);

After I finish manipulating the pixels in "MyPixelsArray" I would like to save its data back into a new PNG image file. Only I can't...

I spent more than a day searching the net but there is no solution to this exact problem. Any help would be highly appreciated. Thanks!

5
  • If you are trying to resize your image, this link maybe helps you stackoverflow.com/questions/1922040/resize-an-image-c-sharp Commented Oct 7, 2016 at 8:46
  • Can you explain what do you exactly want to achieve? Commented Oct 7, 2016 at 8:53
  • What I want to achieve: 1,Load a PNG file into a bytearray (done) 2, Manipulate this byte array thus changing the picture (done) 3, Save this byte array back into a PNG file on the harddrive. (NOT done, hence the question) Commented Oct 7, 2016 at 8:59
  • 1
    Edit your question and add why you can't. Is it throwing any exception? Does it not compile? Is the result PNG wrong? Commented Oct 7, 2016 at 9:55
  • Why not edit the byte of the Bitmap in a LockBits routine?? Commented Oct 7, 2016 at 11:13

1 Answer 1

0

Not tested but might work. Try it i hope it help you

 //Pick a file
        OpenFileDialog FileValaszto = new OpenFileDialog();
         FileValaszto .ShowDialog();

        //Create BitmapImage
        BeolvasottKep = new BitmapImage(new Uri(FileValaszto.FileName));

        //Create byte array
        stride = BeolvasottKep.PixelWidth * 4;
        int size = BeolvasottKep.PixelHeight * stride;

        MemoryStream ms = new System.IO.MemoryStream(stride);
        Image i = Image.FromStream(ms);
Sign up to request clarification or add additional context in comments.

3 Comments

Then do i.Save('filepath', System.Drawing.Imaging.ImageFormat.png);
Thank you all for your kind help!!! Problem is solved.
Hi Broadroad, Could you please mention how you solved? Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.