473,811 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Respons e.Write("No room found");
context.Respons e.End();
return;
}
Bitmap bmp = CreateUpcomingD aysImage();
context.Respons e.ContentType = "image/gif";

bmp.Save(contex t.Response.Outp utStream, 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 CreateUpcomingD aysImage()
{
Bitmap bmp = new Bitmap(_imageWi dth, _imageHeight);
Graphics g = Graphics.FromIm age(bmp);
SolidBrush evenCaptionColo r = new
SolidBrush(Colo r.FromArgb(210, 210, 210));
SolidBrush oddCaptionColor = new
SolidBrush(Colo r.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 (evenCaptionCol or, rect);
else
g.FillRectangle (oddCaptionColo r, 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 2017
<ma**********@g ooglemail.comwr ote in message
news:44******** *************** ***********@v39 g2000pro.google groups.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 CreateUpcomingD aysImage()
{
Bitmap bmp = new Bitmap(_imageWi dth, _imageHeight);
For starters, you should use the Bitmap constructor that takes a third
PixelFormat argument, and pass Format8bppIndex ed to it (though if you're
only using 2 colors, Format1bppIndex ed 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.Ent ries[0] = Color.FromArgb( 210, 210, 210);
bmp.Palette.Ent ries[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**********@g mail.comwrote in message
news:Oa******** ******@TK2MSFTN GP05.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 Format8bppIndex ed to it (though if you're
only using 2 colors, Format1bppIndex ed 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(_imageWi dth, _imageHeight,
PixelFormat.For mat4bppIndexed) ;
bmp.Palette.Ent ries[0] = _colorBookedTim e;
bmp.Palette.Ent ries[1] = _colorFreeTime;
bmp.Palette.Ent ries[2] = _colorPendingTi me;
bmp.Palette.Ent ries[3] = _colorPrePostTi me;
Graphics g = Graphics.FromIm age(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...@g ooglemail.com"
<macap.use...@g ooglemail.comwr ote:
But there is another problem now:

Bitmap bmp = new Bitmap(_imageWi dth, _imageHeight,
PixelFormat.For mat4bppIndexed) ;
bmp.Palette.Ent ries[0] = _colorBookedTim e;
bmp.Palette.Ent ries[1] = _colorFreeTime;
bmp.Palette.Ent ries[2] = _colorPendingTi me;
bmp.Palette.Ent ries[3] = _colorPrePostTi me;
Graphics g = Graphics.FromIm age(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
5176
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 with codebehind, the only reference claiming it can be done i could find in google is this: http://weblogs.asp.net/kdente/posts/10622.aspx I tried using the class name as suggested, to no avail...
5
7086
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 using aspx files because they don't have to be compiled and don't need any page processing. As an experiment, I'm creating an affiliate site that will use ashx files mainly. During testing I haven't noticed anything different other than it seems...
0
1282
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 debug and use intellisense in developing the file. How you guy do in writing ashx?
3
4088
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
6381
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 can I add *.ashx files to the publish target?
0
1947
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 ASHX file, i set the CurrentUICulture for the current thread using the following line, which reads the users' broswer default langauge. This works fine. Thread.CurrentThread.CurrentUICulture = New CultureInfo(context.Request.UserLanguages(0)) ...
0
1656
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 javascript running in the client browser worked fine. Now we have another server with the application on it and the two servers are running in a web farm.
7
2834
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 seems to work fine, however I noticed that no web log entry is added when a successful download occurs (normally a 200 HTTP status code, however, if there is an authorization failure, it gets logged). I have a logging routine that logs a successful...
3
17701
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 begun. So here is JQuery call $.ajax({ type: 'POST', url: url,
0
9734
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9607
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10395
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10408
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9211
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7673
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4346
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.