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

  • Home
  • SEARCH
  • 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 7430277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:06:50+00:00 2026-05-29T09:06:50+00:00

I have to read image binary from database and save this image binary as

  • 0

I have to read image binary from database and save this image binary as a Tiff image on filesystem. I was using the following code

private static bool SavePatientChartImageFileStream(byte[] ImageBytes, string ImageFilePath, string IMAGE_NAME)
    {
        bool success = false;
        try
        {
            using (FileStream str = new FileStream(Path.Combine(ImageFilePath, IMAGE_NAME), FileMode.Create))
            {
                str.Write(ImageBytes, 0, Convert.ToInt32(ImageBytes.Length));
                success = true;

            }
        }
        catch (Exception ex)
        {

            success = false;
        }
        return success;
    }

Since these image binaries are being transferred through merge replication, sometimes it happens that image binary is not completely transferred and we are sending request to fetch Image Binary with a nolock hint. This returns in ImageBytes having 1 byte data and it saves it as a 0 kb corrupted tiff image.

I have changed the above code to :-

private static bool SavePatientChartImage(byte[] ImageBytes, string ImageFilePath, string IMAGE_NAME)
    {
        bool success = false;
        System.Drawing.Image newImage;
        try
        {
            using (MemoryStream stream = new MemoryStream(ImageBytes))
            {
                using (newImage = System.Drawing.Image.FromStream(stream))
                {
                    newImage.Save(Path.Combine(ImageFilePath, IMAGE_NAME));
                    success = true;
                }
            }
        }
        catch (Exception ex)
        {
            success = false;
        }
        return success;
    }

In this case if ImageBytes is of 1 byte or incomplete, it won’t save image and will return success as false.

I cannot remove NOLOCK as we are having extreme locking.

The second code is slower as compared to first one. I tried for 500 images. there was a difference of 5 seconds.

I couldn’t understand the difference between these 2 pieces of code and which code to use when. Please help me understand.

  • 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-05-29T09:06:50+00:00Added an answer on May 29, 2026 at 9:06 am

    In the first version of the code, you are essentially taking a bunch of bytes and writing them to the filesystem. There’s no verification of a valid TIFF file because the code neither knows nor cares it’s a TIFF file. It’s just a bunch of bytes without any business logic attached.

    In the second code, you’re taking the bytes, wrapping them in a MemoryStream, and then feeding them into an Image object, which parses the entire file and reads it as a TIFF file. This give you the validation you need – it can tell when the data is invalid – but you’re essentially going over the entire file twice, once to read it in (with additional overhead for parsing) and once to write it to disk.

    Assuming you don’t need any validation that requires deep parsing of the image file (# of colors, image dimensions, etc) you can skip this overhead by simply checking if the byte[] ImageBytes is of length 1 (or find any other good indicator of corrupt data) and skip writing if it doesn’t match. In effect, do your own validation, rather than using the Image class as a validator.

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

Sidebar

Related Questions

I have read that using database keys in a URL is a bad thing
I have read this post about how to test private methods. I usually do
I have a webapplication where I read images from a sql 2008 database through
I have very little Perl experience. I need to read a binary image in
i'd like to read binary data from a blob, using the Stream interface around
How do I read an image in C so that I can have direct
I have a FAT12 image file and I have to open it and read
I am having problems with django forms and image uploads. I have googled, read
I have a binary image in Matlab, and I need the binary array(0 and
I load a binary image in OpenCV using cvLoadImage as follows: IplImage* myImg=cvLoadImage(<ImagePath> ,-1);

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.