473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Gif Color Indexing in ashx

Hello,
I am creating Gif-Images (8bit: 256 colors) with using the
IHttpHandler and .ashx-Files.

My ProcessRequest-Method looks like this:

public void ProcessRequest(HttpContext context)
{
object o = context.Request["roomId"];
if (o == null)
{
// MP[TODO] ???throw exception???
context.Response.Write("No room found");
context.Response.End();
return;
}
Bitmap bmp = CreateUpcomingDaysImage();
context.Response.ContentType = "image/gif";

bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
bmp.Dispose();
}

But the resulting Gif-Image has Dithering (http://en.wikipedia.org/
wiki/Dithering),
although I only use 2 grey-colors at the moment:


private Bitmap CreateUpcomingDaysImage()
{
Bitmap bmp = new Bitmap(_imageWidth, _imageHeight);
Graphics g = Graphics.FromImage(bmp);
SolidBrush evenCaptionColor = new
SolidBrush(Color.FromArgb(210, 210, 210));
SolidBrush oddCaptionColor = new
SolidBrush(Color.FromArgb(140, 140, 140));

RectangleF rect = new RectangleF();

for (int i = 0; i < _hoursOfDay; i++)
{
rect = new RectangleF(i * _pixel, 0.0F, _pixel,
_pixel);
if (i % 2 == 0)
g.FillRectangle(evenCaptionColor, rect);
else
g.FillRectangle(oddCaptionColor, rect);
}
g.Dispose();

return bmp;
}
Is there a way to avoid Dithering? I am not very sure, but I think
there is a technology
called "indexed colors" in Gif-Files so that the gif file can use 256
different spezified colors.
Can I use this to with the GDI+ Library of .NET? Or what else can I
do?
Regards,

Martin
Sep 4 '08 #1
5 1993
<ma**********@googlemail.comwrote in message
news:44**********************************@v39g2000 pro.googlegroups.com...
But the resulting Gif-Image has Dithering (http://en.wikipedia.org/
wiki/Dithering),
although I only use 2 grey-colors at the moment:


private Bitmap CreateUpcomingDaysImage()
{
Bitmap bmp = new Bitmap(_imageWidth, _imageHeight);
For starters, you should use the Bitmap constructor that takes a third
PixelFormat argument, and pass Format8bppIndexed to it (though if you're
only using 2 colors, Format1bppIndexed might be even better).

After that, Bitmap (or rather, Image, from which it derives) has property
Palette. Before you draw onto the bitmap, you should fill that Palette with
all colors that you intend to use, e.g.:

bmp.Palette.Entries[0] = Color.FromArgb(210, 210, 210);
bmp.Palette.Entries[1] = Color.FromArgb(140, 140, 140);

That should do the trick.
Sep 4 '08 #2
One word of warning:
http://msdn.microsoft.com/en-us/libr...m.drawing.aspx

"Caution:
Classes within the System.Drawing namespace are not supported for use
within a Windows or ASP.NET service. Attempting to use these classes
from within one of these application types may produce unexpected
problems, such as diminished service performance and run-time exceptions."

Marc
Sep 4 '08 #3
"Marc Gravell" <ma**********@gmail.comwrote in message
news:Oa**************@TK2MSFTNGP05.phx.gbl...
One word of warning:
http://msdn.microsoft.com/en-us/libr...m.drawing.aspx

"Caution:
Classes within the System.Drawing namespace are not supported for use
within a Windows or ASP.NET service. Attempting to use these classes from
within one of these application types may produce unexpected problems,
such as diminished service performance and run-time exceptions."
Indeed, and there are good reasons for that as well:

http://blogs.msdn.com/tom/archive/20...d-asp-net.aspx
http://blogs.msdn.com/carloc/archive...a-service.aspx

All in all, it works, you just have to be extra careful with resource
allocation - a missed Dispose() call prove to be very expensive indeed!
Sep 4 '08 #4
Hello Pavel,

thank you very much. This sounds like it is what I wanted
For starters, you should use the Bitmap constructor that takes a third
PixelFormat argument, and pass Format8bppIndexed to it (though if you're
only using 2 colors, Format1bppIndexed might be even better).

After that, Bitmap (or rather, Image, from which it derives) has property
Palette. Before you draw onto the bitmap, you should fill that Palette with
all colors that you intend to use, [...]
But there is another problem now:

Bitmap bmp = new Bitmap(_imageWidth, _imageHeight,
PixelFormat.Format4bppIndexed);
bmp.Palette.Entries[0] = _colorBookedTime;
bmp.Palette.Entries[1] = _colorFreeTime;
bmp.Palette.Entries[2] = _colorPendingTime;
bmp.Palette.Entries[3] = _colorPrePostTime;
Graphics g = Graphics.FromImage(bmp);

This code throws an exception, because I cannot create a Graphics-
Object out of an indexed PixelFormat.

Is there a workaround to do it?

A short google survey says that many people are having this problem
and that there seems to be a very easy solution.
The solutions I found use unsafe code in C# with pointers and so
on ....

Isnīt there really an easy solution to get an indexed bitmap into a
Graphics object?

If there are other solutions... I do need a Graphics object urgently.
I only want to draw rectangles into the indexed bitmap.
If I can do it in another way... it would be fine too.
Regards,
Martin

Sep 4 '08 #5
On Sep 4, 2:46*pm, "macap.use...@googlemail.com"
<macap.use...@googlemail.comwrote:
But there is another problem now:

Bitmap bmp = new Bitmap(_imageWidth, _imageHeight,
PixelFormat.Format4bppIndexed);
bmp.Palette.Entries[0] = _colorBookedTime;
bmp.Palette.Entries[1] = _colorFreeTime;
bmp.Palette.Entries[2] = _colorPendingTime;
bmp.Palette.Entries[3] = _colorPrePostTime;
Graphics g = Graphics.FromImage(bmp);

This code throws an exception, because I cannot create a Graphics-
Object out of an indexed PixelFormat.

Is there a workaround to do it?
None that I know of. You'll have to use LockBits.
A short google survey says that many people are having this problem
and that there seems to be a very easy solution.
The solutions I found use unsafe code in C# with pointers and so
on ....

Isnīt there really an easy solution to get an indexed bitmap into a
Graphics object?

If there are other solutions... I do need a Graphics object urgently.
I only want to draw rectangles into the indexed bitmap.
If I can do it in another way... it would be fine too.
For one thing, if you only need to draw rectangles, then it's not hard
to do that with direct blitting via LockBits.

If you need something more complicated, then consider drawing to a
usual 32-bit Bitmap via Graphics, and then blitting from it to your
own indexed bitmap, looking up colors in the palette as you go.

Sep 4 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bart Adriaanse | last post by:
As VS.NET is not being very supportive in creating a ASHX http handler, i would like to use a codebehind VB file as to get intellisense features etc on it's code. I just cannot get ashx to work...
5
by: Roshawn Dawson | last post by:
Hi, Has anybody created an entire asp.net app using only ashx files? I know that they are simply handlers used by the asp.net worker process. I hear that they are in some respects better than...
0
by: Jason Chan | last post by:
I wanna to create ashx file to generate image on the fly. In VS Add New Item dialog, I cannot find the ashx template. So what I do is create a text file and rename it to .ashx However I cannot...
3
by: Tim | last post by:
Guys I am trying to create an ashx IHttpHandler that will provide images for me. This is what I have done so far. ' Start test.ashx <%@ WebHandler Language="VB" Class="testashx" %>
2
by: Max2006 | last post by:
Hi, After I right-click on my web application project file and choose "Publish ." and do the publishing, the result publishable files does not include the *.ashx files. Is it by design? How...
0
by: nickmarkham | last post by:
Hi, I have 'localized' the button images in my web app by using an HTTP handler file for each image so the correct langauge imagebutton can be loaded depending on the users language. In the...
0
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, We have an asp.net web application with an .ashx handler that is called from javascript from the same web app. We used to run the web app on one server and the ashx calls from the...
7
by: =?Utf-8?B?QU9UWCBTYW4gQW50b25pbw==?= | last post by:
Hi, I have been using the code (some of it has been removed for simplicity) below to allow authenticated (using ASP.NET membership database) users to get a file from their archive area. It...
3
by: George | last post by:
I am doing an AJAX call using JQuery on my page to which returns JSON objects and everything works fine. Now I decided to use ashx handler instead of and simply write JSON out. Then my problems...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.