Skip to content

How to render picturebox image bytearray as jpg and not png? #1311

Closed

Description

@itg-assistant

Ser Gar asked 2018-01-16 05:44:24 UTC

Hi.
I noticed about others posts that the way to load a PictureBox.Image from bytearray is like:
using (var ms = new MemoryStream(_imageBytes))
{
var img = Image.FromStream(ms); //here imageformat stills ImageFormat.Jpeg
_image = new Bitmap(img); // here changes to ImageFormat.MemoryBmp
picBoxImage.Image = _image;
}

But i want smaller response sizes.
Original memory stream is about 70k (jpg encoded), but is rendered to client as png with bigger size (700k), last is a bit slow when 15 or more images
I want to know a way to response as jpg or better approach
Thanks in advance!

Activity

  1. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2018-01-16 17:37:40 UTC

    You need to keep the byte array stream. The code in your message disposes the stream and creates a new bitmap. Do this instead:

    picBoxImage.Image = Image.FromStream(new MemoryStream(_imageBytes));

    Jpeg images in System.Drawing need the stream.

    Otherwise you can also save the image to a temp file and return the name of the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GeneralQuestions, how-tos and everything else.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions