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 7088347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:47:23+00:00 2026-05-28T07:47:23+00:00

I’m using WIA 2.0 to scan images from a HP scanner. The problem is

  • 0

I’m using WIA 2.0 to scan images from a HP scanner. The problem is that saved TIFFs are around 9MBs big (A4 page at 300dpi, grayscale). I convert WIA’s ImageFile that contains scan in TIFF format to BitmapSource like this:

    public static BitmapSource ConvertScannedImage(ImageFile imageFile)
    {
        if (imageFile == null)
            return null;

        // save the image out to a temp file
        string fileName = Path.GetTempFileName();

        // this is pretty hokey, but since SaveFile won't overwrite, we
        // need to do something to both guarantee a unique name and
        // also allow SaveFile to write the file
        File.Delete(fileName);

        // now save using the same filename
        imageFile.SaveFile(fileName);

        BitmapFrame img;

        // load the file back in to a WPF type, this is just
        // to get around size issues with large scans
        using (FileStream stream = File.OpenRead(fileName))
        {
            img = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);

            stream.Close();
        }

        // clean up
        File.Delete(fileName);

        return img;
    }

Anybody have an idea how to reduce image size, if possible in-memory (because I have a lsit of scans that you can preview and rotate)? Thanks.

  • 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-28T07:47:23+00:00Added an answer on May 28, 2026 at 7:47 am

    Use compression. This example Ccitt4 is for black and white fax compression, the compression factor is huge but there are other versions if you want to keep the grey scale.

    using System.Windows.Media.Imaging;
    
    
    public static byte[] ConvertBitmapSourceToByteArray(BitmapSource imageToConvert, ImageFormat formatOfImage)
    {
        byte[] buffer;
        try
        {
            using (var ms = new MemoryStream())
            {
                switch (formatOfImage)
                {
                    case ImageFormat.Png:
                        var bencoder = new PngBitmapEncoder();
                        bencoder.Frames.Add(BitmapFrame.Create(imageToConvert));
                        bencoder.Save(ms);
                        break;
    
                    case ImageFormat.Tiff:
                        var tencoder = new TiffBitmapEncoder();
                        tencoder.Compression = TiffCompressOption.Ccitt4;
                        tencoder.Frames.Add(BitmapFrame.Create(imageToConvert));
                        tencoder.Save(ms);
                        break;
                }
                ms.Flush();
                buffer = ms.GetBuffer();
            }
        }
        catch (Exception) { throw; }
    
        return buffer;
    }
    

    Then to write the image

    doc.SaveDirectory = DestinationDirectoryImages;
    doc.Filename = fName;
    doc.Image = ImageConversion.ConvertBitmapSourceToByteArray(img.Image, ImageFormat.Tiff);
    

    and the implementation of .Image is…

    private byte[] _image;
    /// <summary>
    /// Bytes for Image. Set to null to delete related file.
    /// </summary>
    public virtual byte[] Image
    {
        get
        {
            if (_image == null)
            {
                if (SaveDirectory == null) throw new ValidationException("SaveDirectory not set for DriverDoc");
                string fullFilename = Path.Combine(SaveDirectory, Filename);
                if (!string.IsNullOrEmpty(fullFilename))
                    if (File.Exists(fullFilename))
                        _image = File.ReadAllBytes(fullFilename);
                    else
                        _image = File.ReadAllBytes("Resources\\FileNotFound.bmp");
            }
            return _image;
        }
        set
        {
            if (_image == value) return;
            _image = value;
            if (SaveDirectory == null) throw new ValidationException("SaveDirectory not set for DriverDoc");
            string fullFilename = Path.Combine(SaveDirectory, Filename);
            if (_image != null)
            {
                if (!string.IsNullOrEmpty(fullFilename))
                {
                    _image = value;
                    File.WriteAllBytes(fullFilename, _image);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(Filename) && File.Exists(fullFilename))
                    File.Delete(fullFilename);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.