TextureBrush low quality printing - c#

I'm creating an image with some text and using TextureBrush for draw the text along a line repeatedly. It is working fine on the screen. when i want to print it, it looks very bad.. I've tried to create the image bigger (relative to the dpi differences) and set its resolution as the resolution of the Graphics object (For printing it is higher). Printing the image as it is looks good.. but as TextureBrush not so good.
I'm attaching all the prints - Tried to print it to PDF printer - same results as real printer - so no problem there.
first try before changing anything
Drawing the image using graphics.DrawImage(...) (The image contains The letter 'A' that I've drew in)
Printing TextureBrush
At this time it was clear that i have resolution problem so I've created my image bigger and in better resolution (as the printer dpi)
Drawing the image using graphics.DrawImage(...)
Looks perfect - Exact size and good quality. graphics.DrawImage(...) drew the picture with consideration of its size and resolution.
Now I've used this picture in my textureBrush. The result looks like this
It is bigger because the image itself is bigger - But why it does not consider resolution like graphics.DrawImage(...)..
Anyway i've decided to continue and scale down the image using TextureBrush.ScaleTransform(...) - The results looks like that
This is the best that i've reached so far.. I think the main problem is the TextureBrush that does not consider the resolution of the image.
I would really prefer to stick with the TextureBrush way (And not drawing image by image with clipping and repeating), cause it is very complex code, and complex drawing
Help anyone ?

Related

Change render size in DirectX11

Imagine that I'm working on a 1920x1080 image, doing some computations on those pixels, but I'm interested in showing to the user only a centered part of that rendered image (for example a 500x500).
There is a way to show in my window only a part of the Swapchain using its Present() function ?
I've solved my problem by using a second display doing off-screen rendering at a different resolution. After that rendering, it pass a texture2d to the smaller "real" display which crops it to adapt to the smaller resolution. Thanks anyway!

Cropping a Bitmap to an Area of Interest

Similar to my previous question which I as yet have not solved (Comparing Frames of a live Feed) I have another issue.
Scenario
I have an image taken by a camera that contains a rectangle in it. I need to crop the image to only show the rectangle plus a small margin.
My Efforts
I have accomplished this by iterating through the pixels using LockBits and attempting to find potential edges but these seems terribly slow and inefficient
My Thoughts
I was thinking I could take an empty image as a baseline and then remove the differences between the two, however I cannot be sure that the lighting will be exactly the same and that potential contaminants such as an accidental fly getting into the image will not be present which could muck up this process.
Is there any easier way? The rectangle should (usually) be in the bottom left corner, but not always (long story) but this cant be relied upon.
My Environment
Visual Studio 2012 (2010 if neccessary is available)
Ueye camera
C#
The images are of type System.Drawing.Bitmap
The rectangle will often be something like a credit card or an ID card or anything of a similar size and shape
The empty image (background) looks like this:
Using EmguCV you can detect shapes such as a rectangle. Click here for the emgu code. Once you have detected the rectangle it is fairly easy to crop it out using a new Bitmap with the size of the rectangle.
The sample demonstrates how to crop the image from specific Picturebox control into destination Picturebox control using mouse selection or specified coordinates.
1.How to use mouse to select an area (rectangle) in a Picturebox control.
2.How to crop the image by the rectangle.
http://code.msdn.microsoft.com/windowsdesktop/CSWinFormCropImage-d4beb1fa

How To Handle Image as Background to CAD Application

My application is used to design airports for a flight simulator. Users can add one or more images as background. The image(s) can be sized accurately and then used as a template to lay down features such as runways, aprons and so on.
I use a third party graphics library (Piccolo) which has an image class (as far as I can see it is a simple wrapper for System.Drawing.Image).
So far I have done little except allow the user to add an image, size it and so on. It will be no surprise that users sometimes complain of poor performance. We tell them not to load large images (up to 100k seem OK) but don't stop them and 100Mb bitmaps have been used with horrible results.
I need to fix this in a couple of ways. First by converting any image they use to an efficient format (size vs definition) and second by ensuring that the loaded image is suitably sized for the dimensions - at the moment I don't do anything specific to deal with the resizing of say a 2000 x 2000 image to fit a 500 x 500 area of the display.
The default 1:1 display of the application represents 1m per pixel. Once the user has resized the image to fit accurately would I be right in thinking that the best resoultion for the image would be to resample it to that size? I am aware that if the user zooms in way past 1:1 which they will probably do then the clarity of the image will fall.
My ignorance of handling images is complete. I have looked at some image manipulation libraries (ImageMagick and the free version of dotIamge) first for converting the input image to a standard one and second for resizing -resampling. The truth is that they do far more than I need.
Any pointer much appreciated.
Yes, resampling so that the bitmap doesn't constantly have to be rescaled for every paint should make a big difference. The default Graphics.InterpolationMode makes pretty images but is not cheap.
Another biggie is the pixel format of the bitmap. Format32bppPArgb is 10 times faster than any of the others on most video adapters.
private static Bitmap Resample(Image img, Size size) {
var bmp = new Bitmap(size.Width, size.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
using (var gr = Graphics.FromImage(bmp)) {
gr.DrawImage(img, new Rectangle(Point.Empty, size));
}
return bmp;
}

imageList.Draw doesn't draw with different size?

The ImageList has a method named "Draw":
imageList.Draw(graphics, bounds.X, bounds.Y, bounds.Width, bounds.Height, imgIndex);
I use this method to draw an image on a graphics object of a PrintDocument. When using the original image size (16 x 16 pixels), the image is drawn correct. If however, I change the bounds size, nothing is drawn. Even changing the size to 32 x 32 (double size) has no effect. Nothing is drawn. I need to change the drawn size because of the different dpi ... Where am I gong wrong ?
Edit: The solution seems to be simply to use the g.DrawImage method instead. Why imageList.Draw() doesn't draw is still a mistery to me ...
g.DrawImage(imageList.Images[imgIndex], bounds);
ImageList.Draw() is a bit unusual, it takes advantage of the built-in support that the native image list code inside of Windows has for rendering an image in the list. This is an optimization, it avoids the cost of converting the internal image as stored in the native image list back to a managed Image object.
One side-effect however is that this drawing happens without regard for any of the transforms that were applied to the Graphics object. A 16x16 image in the list is going to be rendered as 16x16 pixels on paper. Which is indeed a bit hard to find back, printers have very high resolution (600 dots per inch is typical), that image turns into a decimal point.
Image lists were really meant to be the source of images for the TreeView and ListView controls, it is not a good general purpose collection object for images. Like a List<Image>. Your workaround is good, the Image property converts the internal bitmap back to a managed Image, Graphics.DrawImage() will then scale it appropriately to get a size on paper that's close to the size on the screen. However with the graininess you get from making an image 6 times larger. Note that you should Dispose() that object.

How to convert a 32bpp image to an indexed format?

So here are the details (I am using C# BTW):
I receive a 32bpp image (JPEG compressed) from a server. At some point, I would like to use the Palette property of a bitmap to color over-saturated pixels (brightness > 240) red. To do so, I need to get the image into an indexed format.
I have tried converting the image to a GIF, but I get quality loss. I have tried creating a new bitmap in an index format by these methods:
// causes a "Parameter not valid" error
Bitmap indexed = new Bitmap(orig.Width, orig.Height, PixelFormat.Indexed)
// no error, but the resulting image is black due to information loss I assume
Bitmap indexed = new Bitmap(orig.Width, orig.Height, PixelFormat.Format8bppIndexed)
I am at a loss now. The data in this image is changed constantly by the user, so I don't want to manually set pixels that have a brightness > 240 if I can avoid it. If I can set the palette once when the image is created, my work is done. If I am going about this the wrong way to begin with please let me know.
EDIT: Thanks guys, here is some more detail on what I am attempting to accomplish.
We are scanning a tissue slide at high resolution (pathology application). I write the interface to the actual scanner. We use a line-scan camera. To test the line rate of the camera, the user scans a very small portion and looks at the image.
The image is displayed next to a track bar. When the user moves the track bar (adjusting line rate), I change the overall intensity of the image in an attempt to model what it would look like at the new line rate. I do this using an ImageAttributes and ColorMatrix object currently.
When the user adjusts the track bar, I adjust the matrix. This does not give me per pixel information, but the performance is very nice. I could use LockBits and some unsafe code here, but I would rather not rewrite it if possible. When the new image is created, I would like for all pixels with a brightness value of > 240 to be colored red. I was thinking that defining a palette for the bitmap up front would be a clean way of doing this.
Going from 32bpp to 8bpp indexed will almost always result in quality loss, unless the original image has less than 256 colors total.
Can you create another image that is a overlay with the affected pixels red, then show both of those?
Since you are going for brightness > 240, you can convert the overlay to grayscale first, then to indexed to get the overbright pixels.
You don't specify what you are doing with it once you have tagged the offenders, so I don't know if that will work.
Sounds like something you could do easily with a pixel shader. Even very early shader models would support something as easy as this.
The question is however:
Can you include shader support in your application without too much hastle?
Do you know shader programming?
EDIT:
You probably don't have a 3D context where you can do stuff like this =/
I was mostly just airing my thoughts.
Manipulating the picture pixel by pixel should be doable in real-time with a single CPU shouldn't it?
If not, look into GPGPU programming and Open CL.
EDIT AGAIN:
If you gave some more details about what the app actually does we might help a bit more? For example, if you're making a web-app none of my tips would make sense.
Thanks for the help everyone. It seems that this can be solved using the ImageAttributes class and simply setting a color remap table.
ColorMap[] maps = new ColorMap[someNum]
// add mappings
imageAttrs.SetRemapTable(maps);
Thanks for the help again, at least I learned something.

Categories