In my ASP.NET application I use a web form to upload an image file to the server.
I get the file,
create a Bitmap object from it,
rescale the Bitmap if I need to,
create another Bitmap file which is the thumbnail of the first one,
then I use your code to convert both into JPEG,
then I convert both into byte,
then I save them into an Image field in a DB (SQL Server 2000).
The file that I upload is 45Kb, but the end file in the DB is 500Kb.
Any ideas why this is happening? Any ways to avoid it?
I'm not sure why you're having this problem; I'd have to see your code to do a more thorough diagnosis. My first inclination is to examine the JPEG quality setting you're using; if you convert to JPEG with a quality value on or around 100, the resulting file size can be quite large.
Is there a problem with .net DrawImage and metafiles?
I am having a problem using DrawImage to scale metafiles. Here is what
I am doing:
Metafile mapImage = new Metafile("C:\\image.wmf");
If I draw the entire metafile it draws correctly:
g.DrawImage(mapImage, 0, 0);
If I try to scale the metafile into the graphic, it does get scaled
but tho origin is moved so what gets drawn in position 0,0 is
not position 0,0 in the metafile even though both rectangles have 0,0
g.DrawImage(bitmapMap, new Rectangle(0, 0, bitmapDest.Width,
bitmapDest.Height), new Rectangle(0, 0, mapImage.Width,
In fact, I thought the above statement SHOULD display the entire metafile but it only displays a portion on the lower right corener of the metafile.
Also, if instead of using a metafile, I use a Bitmap, then I don't have
this problem (i.e Bitmap mapImage = new Bitmap("C:\\image.bmp"))
maybe it isn't 100% related to the topic, but... do you have somebody an idea, how to convert an Image to the another colour depth? Let's say 800x600x24bit to 800x600x8bit in grayscale. GDI is really pain in ass for such tasks.
Unfortunately, lack of dithering functionality is one of my biggest complaints about GDI+. I keep meaning to extend the bitmap manipulation class to implement a few well-known dithering algorithms, but of course I never sem to have time.
Another issue is that high-performance dithering requires direct access to the pixel data, which afaik requires use of unsafe pointers, which breaks CLI compliance, and prevents unprivileged users (for example, on shared hosting platforms) from using the solution. On the other hand, a safe implementation would be markedly slower.
maybe it isn't 100% related to the topic, but... do you have somebidy an idea, how to convert an Image to the another colour depth? Let's say 800x600x24bit to 800x600x8bit in grayscale. GDI is really pain in ass for such tasks.
I don't have any problem on your Manipulation Class.
You know what , I want to convert any type of bitmap or any type of Image to Monochrom Bitmap (1 bit per pixel )and then get the bits of the bitmap (For example 00011010100101). OK...
So when I saw your class I thought that maybe u can help me to solve my problem.;)
I don't know that the stock Bitmap class supports monochrome bitmaps. There is no PixelFormat value for monochrome, and in my tests of the bitmap manipulator I was unable to load monochrome bitmaps without runtime errors from the Bitmap class implementation.
In general, I've found the GDI+ classes to be sadly lacking in functionality; this is but one example of that lack.
I'm sorry I can't answer your question; perhaps another reader can. However, absent an elegant solution, you might consider converting to "monochrome" yourself. A monochrome conversion can be fairly easy to implement, by selecting a "threshold" value then setting all pixels above that threshold to "1" or "white", and all below to 0 or "black". Though the image will remain "color" as far as the Bitmap file format is concerned, it will contain only two colors, black and white. You could then produce a BitArray from the pixel data, again manually.
I admit this a kludge (at best), but depending upon your requirements it may solve the problem. I hope you're able to find a more elegant solution elsewhere.
I'm planning a follow-up piece to this article, which will add color depth conversion capabilities, which GDI+ is painfully lacking. Perhaps this addition will provide a partial solution to your problem.
I have seen your answer, and I realy thanks.
I thought about your suggestion but I didn't find the method to get Threshold value. Anyway I will find something to solve it. But I want to tell you something about my last experience.
I converted the bitmap to Monochrome format in Visual C++, with an API function "LoadImage". As you know this function returns a Handle to Bitmap (hBitmap) and use this handle in CBitmap MFC class and I’ve got the result OK. But I used this function in C# and when I wanted to show it on the PictureBox, an Unhandled Exception is appeared.
The code is something like this:
public static extern IntPtr LoadImage(
IntPtr hinst, // handle to instance
string lpszName, // image to load
uint uType, // image type
int cxDesired, // desired width
int cyDesired, // desired height
uint fuLoad // load options
Regarding the method I suggested, there is no "method" to select the threshold per se; rather, this is a parameter selected by you. For example, imagine a 256-shade grayscale image. Each pixel is specified by an 8-bit gray scale value. You might decide that all pixels having gray scale values > 128 are "white", and those <= 127 are "black". For color images, you'd convert the RGB triplet into a single value before applying the threshold, much like you'd dither a color image to grayscale.
Regarding the code you posted, what is the exception you are encountering? Are you certain the function "LoadImage" exists in User32.dll? My MSDN docs indicate there should be two versions of "LoadImage": the ANSI version called "LoadImageA" and the Unicode version, "LoadImageW". Try replacing "LoadImage" with "LoadImageW".
the dll import attribute will decide itself wether it should search for an ansi or unicode version of a function. You can specify a wether you'd like to import ansi or unicode version by adding parameters to your dllimport attribute:
[DllImport("user32.dll", ExactSpelling=true, ....)]
Default for ExactSpelling is false, so best matching function will be imported (means best matching for current enviroment: unicode on nt and ansi on non-nt).
Great article. It covers a lot of the basics. The problem I am running into is that GDI does all of it's operations in a common underlying pixelformat (I think 32bpp), so if I load in a GIF with transparency, which generally is 8bppIndexed or another Indexed type, and do any kind of resizing or thumbnail modification, the transparency is lost.
To test this in your app, just load a gif with a transparent background and resize it. The background becomes solid black after the conversion.
I've seen some related solutions elsewhere that contain unsafe individual pixel copying to solve this issue, but I am still trying to get something a little more ideal.
The app itself is just a quick and dirty demonstration of the capabilities of the underling class. If you're looking to use it in a production application, you'd do well to consider writing your own shell application instead of using my hacked-together version.
1. Just use the OpenFileDialog class in .NET to prompt for a file.
2. Use Graphics.DrawString to draw text to a graphics object. See my application code for how to bind a Graphics object to a Bitmap, perform the operations, then get the Bitmap back.
3. Depending upon what you mean by eraser, that could be rather complex. You could just implement the "eraser" as a white brush, in which case just rip off the source code for any of the .NET sample programs that demonstrate painting with the cursor, and use their techniques to draw white wherever the cursor is dragged.
4. This will be the trickiest of all. The best way will be to post the image to an ASP.NET page (or Web Service), then within ASP place the image wherever you want it to be.
Alternatively, you can try to FTP it to a folder on your site, but the problems with this solution are many.
If you tell me what you're trying to do, I might be able to point out a better approach.