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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:48:09+00:00 2026-06-05T23:48:09+00:00

I have a function like the following: // Convert To JPG // public string

  • 0

I have a function like the following:

    // Convert To JPG 
    // 
    public string AlltoJPG(FileInfo foo)
    {
        // Get file extension
        string fileExtension = foo.Extension;

        // Get file name without extenstion
        string fileName = foo.Name.Replace(foo.Extension, string.Empty) + ".jpg";

        /*------------------------------------------------------------------------*/

        /// <Check for PNG File Format>
        if (fileExtension == ".png" || fileExtension == ".PNG")
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);

            // Assumes img is the PNG you are converting
            using (Bitmap b = new Bitmap(img.Width, img.Height))
            {
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b))
                {
                    g.Clear(System.Drawing.Color.White);
                    g.DrawImageUnscaled(img, 0, 0);
                }

                // Save the image as a JPG
                b.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }

        }

        /*------------------------------------------------------------------------*/

        /// <Check for GIF File Format>
        if (fileExtension == ".gif" || fileExtension == ".GIF")
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);

            // Construct a bitmap from the image resource.
            Bitmap bmp1 = new Bitmap(img.Width, img.Height);

            // Save the image as a JPG
            bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        /*------------------------------------------------------------------------*/

        /// <Check for BMP File Format>
        if (fileExtension == ".bmp" || fileExtension == ".BMP")
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);

            // Construct a bitmap from the image resource.
            Bitmap bmp1 = new Bitmap(img.Width, img.Height);

            // Save the image as a JPG
            bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        /*------------------------------------------------------------------------*/

        /// <Check for TIFF File Format>
        if (fileExtension == ".tiff" || fileExtension == ".TIFF")
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);

            // Construct a bitmap from the image resource.
            Bitmap bmp1 = new Bitmap(img.Width, img.Height);

            // Save the image as a JPG
            bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        /*------------------------------------------------------------------------*/
        fileName = foo.DirectoryName + "\\" + fileName;
        return fileName;
    }

I’m trying to convert bmp,png,gif,tiff file formats to jpg, but GDI+ as gives:

System.OutOfMemoryException was unhandled
Message=Bellek yetersiz.
Source=System.Drawing
StackTrace:
konum: System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
konum: System.Drawing.Image.FromFile(String filename)

So how can I avoid this and convert at least PNG and BMP files to JPG?

Here is a picture of the error:

enter image description here

  • 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-05T23:48:11+00:00Added an answer on June 5, 2026 at 11:48 pm

    I found the problem! It was caused by PNG alpha channel..

    Thanks to max, link below solved my problem:

    How to load Transparent PNG to Bitmap and ignore alpha channel


    public static void SetAlpha(this Bitmap bmp, byte alpha)
    {
        if(bmp == null) throw new ArgumentNullException("bmp");
    
        var data = bmp.LockBits(
            new Rectangle(0, 0, bmp.Width, bmp.Height),
            System.Drawing.Imaging.ImageLockMode.ReadWrite,
            System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    
        var line = data.Scan0;
        var eof = line + data.Height * data.Stride;
        while(line != eof)
        {
            var pixelAlpha = line + 3;
            var eol = pixelAlpha + data.Width * 4;
            while(pixelAlpha != eol)
            {
                System.Runtime.InteropServices.Marshal.WriteByte(
                    pixelAlpha, alpha);
                pixelAlpha += 4;
            }
            line += data.Stride;
        }
        bmp.UnlockBits(data);
    }
    

    Usage:

    var pngImage = new Bitmap("filename.png");
    pngImage.SetAlpha(255);
    

    after setting alpha it started to work correctly.

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

Sidebar

Related Questions

I have a function that is declared like so: public static string MultiWhereToString(List<WhereCondition<T>> whereConditions)
i have a variable like the following and i want a function to only
i have the following function in Form1 and would like to call it when
I have the following function that takes a number like 5 and creates a
I have function like this typedef vector<vector<string> > vecArray; vecArray dul(); vecArray dul() {
I have the following jquery I'd like to convert to prototype. I am having
Possible Duplicate: convert string to 2D array using php I have the string like
Hi i have the following code which i would like to convert to C#.
I have a JavaScript object that looks like the following: venue = function(map, dataSet)
Suppose I have a the following in Scala object Foo { var functions: List[String

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.