Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:13:03+00:00 2026-06-17T09:13:03+00:00

What i am trying to do is to convert an image into a byte

  • 0

What i am trying to do is to convert an image into a byte array and then write that byte array into a file. here’s the code

public static byte[] Convert(Image img)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            // or whatever output format you like
            return ms.ToArray();
        }
    }

    public Form1()
    {
        InitializeComponent();
        Bitmap pic = new Bitmap("tulips.jpg");
        pictureBox1.Image = pic;
        byte[] img_array;
        img_array = Convert(pic);
        File.WriteAllBytes("test.txt", img_array);

    }

Now I have been successfully able to convert the image into byte array. I have checked the values in byte array by means of a breakpoint and all of them are valid.

However when I try to write the array into a file and then open the file all I see is garbage.

Am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:13:04+00:00Added an answer on June 17, 2026 at 9:13 am

    Your Bitmap is an Image. Why do you convert it into a byte array when you can simply call Save (documented here)

    Bitmap pic = new Bitmap("tulips.jpg");
    pictureBox1.Image = pic;
    pic.Save("test.gif", System.Drawing.Imaging.ImageFormat.Gif); 
    

    To manipulate the image, you will usually access the pixel data – what you have now contains the file type header as well as the pixels! See Bitmap.LockBits to manipulate the pixels (documented here)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to read jpg file and convert into byte array this is
Hi I'm trying to convert image to byte array to pass it into sql
I am trying to convert a PIL image into an array using NumPy. I
I'm trying to convert wmf image file into png format with c#.net. But, saved
I'm trying to convert a bitmap image into an uncompressed tif file for use
I'm trying to convert an image file into a binary file, where I'm using
I'm trying to convert a grayscale image to a binary. The code I have
So I was trying to convert a Bitmap image to JPEG in code and
I am trying to convert emoticon codes into emoticon images. I want to write
I'm trying to convert an image into base64 using javascript, however if I request

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.