How to save an 8-bit grayscale JPEG image?

Hi,
I have a problem with saving JPEG images in 8-bit indexed pixel
format. When I save a bitmap in any other format (gif, tiff, png, bmp)
everything works ok, but for some reason it doesn't work on JPEGs...
This is the code I use for saving bitmaps:

MemoryStream mss = new MemoryStream();
FileStream fs = new FileStream(filepath, FileMode.Create,
FileAccess.ReadWrite);

            if (format == ImageFormat.Jpeg)
            {

                ImageCodecInfo[] codecs =
ImageCodecInfo.GetImageEncoders();

                ImageCodecInfo ici = null;
                foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.MimeType == "image/jpeg")
                        ici = codec;
                }

                EncoderParameters ep = new EncoderParameters(2);
                ep.Param[0] = new
EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)85);
                if(MyBitmap.PixelFormat ==
PixelFormat.Format8bppIndexed)
                {
                    ep.Param[1] = new
EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 8L);
                }
                else
                    ep.Param[1] = new
EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 24L);
                MyBitmap.Save(mss, ici, ep);
            }
            else
                MyBitmap.Save(mss, format);

            byte[] array = mss.ToArray();
            fs.Write(array, 0, array.Length);

            mss.Close();
            fs.Close();
            mss.Dispose();
            fs.Dispose();

In the debugger I can see that the in-memory bitmap is in
Format8bppIndexed, so the line that executes is this one:

ep.Param[1] = new
EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 8L);

But this has no effect on the output JPEG file. Any ideas why?
0
moss
12/14/2009 7:59:02 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

1 Replies
4418 Views

Similar Articles

[PageSpeed] 49

moss wrote:
> [...]
> In the debugger I can see that the in-memory bitmap is in
> Format8bppIndexed, so the line that executes is this one:
> 
> ep.Param[1] = new
> EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 8L);
> 
> But this has no effect on the output JPEG file. Any ideas why?

AFAIK, JPEG doesn't support indexed image formats.  The image is 
probably getting converted back to an RGB format as part of the save.

You can Google for documentation on the JPEG format to confirm.

Pete
0
Peter
12/14/2009 8:27:59 PM
Reply:

Similar Artilces:

error message #8
How do I install Money 2004 and set it up to use Norton Utilities? In microsoft.public.money, bkoch wrote: >How do I install Money 2004 and set it up to use Norton >Utilities? Try it? If no go, read various opinions and experiences that have been posted: http://groups.google.com/groups?as_q=&num=50&as_scoring=d&hl=en&ie=UTF-8&oe=UTF-8&btnG=Google+Search&as_epq=&as_oq=nav+norton&as_eq=google&as_ugroup=microsoft.public.money&as_usubject=&as_uauthors=&as_umsgid=&lr=&as_drrb=q&as_qdr=&as_mind=12&as_minm=5&as_mi...

Epson Action Laser & Win 7-64 bit
I think the answer is using my AL 1500 is a lost cause, but here goes. I have this fancy new custom built PC with Win7- 64 bit HP and I love it and the OS so far. What I didn't realize when I ordered the PC with the Asus motherboard is that there's no parallel port. My 15 yr old Epson Action Laser 1500 is my workhorse, but I haven't been able to trick Windows into thinking it's an HP LaserJet III like some folks have been able to do. I got an LPT to USB cable and in hooking it up, Windows shows Unidentified Printing Device, and "this device is working p...

great palins 8.0 us payroll
can i see a traning demo session on us payroll 8.0 ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "I Agree" in the message pane. http://www.microsoft.com/Businesssolutions/Community/NewsGroups/dgbrowser/en-us/default.mspx?mid=64dd06be-2f56-4c41-91cc-e1c977be182a&dg=microsoft.public.greatplains ...

Searching for Image and adding path
I am working on a database that has several records. Each record has a different image associated with it (or will). I will not be the only person using this database, so I want to make it very simple to add images to records. I know that saving the image in the database isn't the best idea for Access 2003, so instead I want to have the path stored (which will make mail merging work too in Word). This is what I want: In the form, on the tab where the image information is, I want 3 things... - The image itself displayed, once the user adds it - The PATH of the image in a text b...

Hyperlink #8
Does anyone know why I cant insert a hyperlink in a shared worksheet? Hi this is a restriction of shared workbooks. Only hyperlinks inserted BEFORE sharing the workbook work, no chance to insert new ones -- Regards Frank Kabel Frankfurt, Germany Nick wrote: > Does anyone know why I cant insert a hyperlink in a shared > worksheet? ...

Automatic Files Saves
In previous versions of Excel it was possible to setup periodic automatic saves that would prompt the user to save. I am not looking for the auto-recovery save. Is there an add-in or update that can be installed to do the same thing in Version 2002. ...

Reading and writing UTF-8 files
I have to do some simple text editing to large-ish (2 Mbyte) html files generated by Word. They are, I believe, in UTF-8. It is the !%^&* problem where single apostrophes become sequences of funny characters, some spaces are shown as unprintable characters, etc. The following code just reads and writes a file, and shows the problem. It applies whether I use a String or a StringBuilder, and whether or not I explicitly force UTF-8 encoding. Can somebody just tell me basically how to copy an html file by reading it in and then writing it out, which is all the following meth...

Excel File Edited and Saved in Word
A user mistakenly opend a excel file in word, edited it, and then saved with the excel extention in word. Now this file could not be opened in excel. Is there any way to get the file back in excel? Thanks a lot in advance for any help!!! Sorry, but that file is gone. I hope you have a backup or have sent it to someone as an attachment. -- Greeting from the Gulf Coast! http://myweb.cableone.net/twodays "Ping" <anonymous@discussions.microsoft.com> wrote in message news:0d8701c3a3dc$185b38f0$a001280a@phx.gbl... > A user mistakenly opend a excel file in word, edited it, &...

How do I save a picture from word into my pictures?
I have recently installed Office 2007. I am unable to copy and paste a picture from word 2003 onto publisher 2007. So I tried to save the picture into my pics and then insert but I can't do that either. Please help. Unique wrote: > I have recently installed Office 2007. I am unable to copy and paste > a picture from word 2003 onto publisher 2007. So I tried to save the > picture into my pics and then insert but I can't do that either. > Please help. ============================ Maybe the following links will help: (555171) WD: How To Extract Embedded Im...

saving the same file in different places at a time
hi, i have an excel file in one system when i update this file, is it possible some range of cells in this file to be saved in the different system also how do i give a link to the other file? appreciate, if anyone could help Sounds like you want to paste a link so that the cell will update every time the other cell gets new info... To do this, say you want to link cell A1 to cell A7...click on cell A7 and click copy...Go to cell A1 and click paste...Then (if you have Excel 2003), click on Edit/Paste Special/Paste Link and you now have youe link...The "Paste Link" Button ...

Percentages #8
Hi there, hope somebody out there in cyberworld can help me with the following probably easy to answer post. I have a number in box A1 lets say 100. This number is what I have dealt with over the month. In box B1 I have 10 and in box C1 I have 10. The numbers in B1 and C1 are figures within the 100 that I have missed for the month. How do I calculate out of 100% what I have achieved. I know how to calculate what percentage I have missed, i.e =sum(B1:C1)/A1*100 = 20%. ACHIEVED A1 = 100 MISSED B1 = 10 + C1 = 10 PERCENTAGE ACHIEVED = ? Thanks in advance for any help given. Terry ...

How To Show Drag Image while Dragging Item?
I use MFC 4.2. There are 2 CListCtrls in my app. I am implementing a drag and drop function from one list to another. How to show the drag image when I still choose which item that will accept the drop? Thank you. You can use a CImageList and store a drag image. Look at functions like CImageList ::BeginDrag, CImageList::BeginDrag | CImageList::EndDrag | CImageList::DragMove | CImageList::DragLeave. -Seetharam This article may be interesting to you. It's a little older, but it is still useful information: http://www.codeguru.com/cpp/controls/listview/dragdrop...

IE 8 Parsing issues?
Hello, since a few month we're having strange requests in our web server logs. Example: 2010-01-27 18:00:35 W3SVC183691552 194.183.128.104 GET /SysRes/GLOBALSkin/Ski/head><body+onload= - 80 - 194.183.153.12 Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0) http://my.domain.at/ 200 0 3 21699 757 46 Sometimes embedded resources like Scripts or CSS files are not loaded correctly. The path to the external resources is broken by adding any HTML code snippet of the page which embeds the resources. It happens on different pages / applications on our serv...

Auto save / data recovery
My pc has crashed without me having saved my flowchart, does Visio have an auto save function and is there any way I can recover my data. Regards Rob Under Tools \ Options \ Save, there is Autosave available but it is not turned on by default -- -ronnie [MS] This posting is provided "AS IS" with no warranties, and confers no rights. "Rob Stevenson" <rob.stevenson@prestigemedical.co.uk> wrote in message news:0a4101c36c8c$767a3e90$a501280a@phx.gbl... > My pc has crashed without me having saved my flowchart, > does Visio have an auto save function and is ...

Send and save a message in folders
Is it possible to send and save a message in a folder like it is with Lotus Notes? A simple function asking in which folder I would to save my send mail. I would like to save all messages to one adress in one folder. Anders, As far as i know Exchange doesn't have a function like that. In outlook you can creat a rule that stores sent messages to specific custom made folders in Outlook Rgds, Jelle There's no setting to ask on sending but you can have rules move sent messages based on criteria "anders bitzer" <andersbitzer@discussions.microsoft.com> wrote in messa...

How do I set up a template to save time typing names
I have to send a lot of e-mails to the same people, and always more than one person. How do I set up a template so that I don't have to keep typing who it is to and who I am 'Cc' to? traceyella <traceyella@discussions.microsoft.com> wrote: > I have to send a lot of e-mails to the same people, and always more > than one person. How do I set up a template so that I don't have to > keep typing who it is to and who I am 'Cc' to? Create your message, but instead of sending it, save it as an OFT. WHen you want to send it, forward it from your Drafts fold...

Conditional format for image or figure
I am looking for a way to relate the format (filling colour) of an image or figure that is imported into excel. This would allow me to design simple maps with coloured codes for presentations of regional reports. Anyone? in Dutch: Ik wil dus bescheiden GIS kaartjes maken in excel zonder dat ik een veel te duur GIS programma moet opsnorren. (bijvoorbeeld de regio's met een goed verkoopcijfer groen, de regio's met verlies rood,...) Alvast bedankt ...

Make Bit field control value of float field
Hi. I've made bit field with the default options of yes and no: new_Q1 Then i've made a field to collect a amount of points, incase the bit is set to yes: new_Q1p. If i select the format of the bit value to "list" my script works fine. If I select "check box", it does not work. Can any one help me modyfi the script to work with the check box? Thanks Regards, Jacob Mondrup This is the script I use: ______________________________________________________________________________ var point = 0; var get_status = parseInt(crmForm.all.new_q1.DataValue); switch (get...

Access2007 ON_CLICK problem on IMAGE on a TAB CONTROL
Hi All, With Access 2003 i made a form with a tab control. One one of the pages there is an image control with an ON_CLICK event assigned to it (normal vba procedure). With Access2003 the ON_CLICK is executed correctly Exactly the same DB run under Access 2007 the ON_CLICK event is ignored...no reaction at all....... This problem does not seem to occur if the image is not on a tab control Nor does it appear when i replace the image with a Button control. Can anyone tell what is hapening ? Rob, this is a known issue, but it was fixed in the service pack Microsoft released this week. You c...

Saving Word attachment
I cannot save an open attached Word document in Outlook. The top bar flashes and I have to press control+Alt+delete to turn it off. The only way I can save it is to open Word program outside of Outlook, and then go to outlook, and then save the attached Word document. What's wrong? I can save all other Office documents...excel, powerpoint with no problem. How can I fix the problem? ...

How to save settings for my printer to print doublesided from exce
When I print doubled sided in word everything goes fine but whenever I try to print doublesided from excel and I go for advanced on my printer and check the boxes for it and then klick ok - after that I go in there to check that it's ok and then all my settings are gone.. Why and how can I change this so that I can print double sides in excel aswell? ...

PDC module in Great Plains 8
Hi i just wonder do we have a Post Dated Checks solution as a module in Great Plains 8 regards Nader How do you want it to work? "Nader Yousef" wrote: > Hi > > i just wonder do we have a Post Dated Checks solution as a module in Great > Plains 8 > > regards > Nader When a customer pays for his goods he pays the cashier a cash deposit which should be recorded as a CRV and the balance is to be payed by a Post Dated Checks with different due dates and the customer account is credited and debit the PDC account and when the check is deposited and acce...

Export Image Package
I have an access application that has stored .JPG images within the database as a "Package". How can I export the image back to a normal .jpg file so that it can be read by a normal (non ole server) imaging application? ...

Can I save and email a merged schedule
I've been able to merge the schedules but I've been unable to send the consolidated file via email. What am I doing wrong? Remove the links and e-mail it. Doubleclick on the inserted projects, go to the Advanced Tab, and deselect the option to link to a file. That will embed the inserted project into the master. - Andrew Lavinsky Blog: http://blogs.catapultsystems.com/epm > I've been able to merge the schedules but I've been unable to send the > consolidated file via email. What am I doing wrong? > ...

exporting/saving recent files list
I'm getting a new computer. Is there a way to save the "recent files" list that is available in each of my Office programs? (Does anyone know where Office saves these shortcuts?) http://groups.google.com/group/microsoft.public.excel.programming/browse_thread/thread/bd58b46fe9c11ef9/853766524964c427?lnk=gst&q=recently+opened#853766524964c427 -- Gary''s Student - gsnu200909 "vabki" wrote: > I'm getting a new computer. Is there a way to save the "recent files" list > that is available in each of my Office programs? (Do...