Blur images shown in listview - c#

In my winform app, i am adding images to listview using ImageList.
but when i increase the Image size (i.e. height & width) small images(ex. bmp images) got blurred.
is there any way to prevent imgs from blurring ??
Thanks in advance.

In .NET ListView, you probably have to wrap the image in standard-sized bitmap and then put the whole bitmap in the item:
Bitmap bmp = new Bitmap(64, 64);
Graphics grfx = Graphics.FromImage(bmp);
grfx.DrawImage(
myImage,
(bmp.Width - myImage.Width) / 2,
(bmp.Height - myImage.Height) / 2);
listView.Items[0].Image = bmp;
You may try Better ListView Express or Better ListView. It supports images of arbitrary sizes out of the box. When the image is small, it puts it on center without resizing and prevents the blurring:
enter link description here

If those are small images then obviously it will be blurred. If you want bigger images and you cant find them on Internet then try to create your own images by outlining the small images in photoshop or gimp(free product).

No there isn't. If you're making images bigger than their original size they always lose quality. Try using bigger images at first.

Related

Zoom performance in C# Bitmap

I'm working with C# WinForms app and using custom control for displaying bitmap images. The problem is, when zooming images I'm using MouseWheel event and, creating new Bitmap image, based on new width and height properties.
On small and average images that works fine, but when size of image is about 3000*3000 pixels or greater, I have serious performance problems in line
scaledImage = new Bitmap(baseImage, size);
Where size - new size of image and baseImage - my original .png image.
So, maybe exists some another solution for such scaling?
Also I must mention that I'm limited with .net 3.5

C# Why are some bitmaps resized after copying using GDI+?

I have a Bitmap variable and I copy smaller 32x32 png files (loaded as Bitmaps) onto the bitmap. However, some png's are scaled up (always the same ones) and appear as 36x36 for example after copying. Almost as if some png's have another DPI or something? How can I prevent this?
Graphics g = Graphics.FromImage(destinationImage);
g.DrawImage(sourceImage, location); // sourceImage is sometimes larger than it actually is. On disk it is 32x32 but after copying it might be bigger...
g.Dispose();
I guess you are right about DPI, as it's stated in the documentation:
This method draws an image using its physical size...
I'm too lazy to make a test project, but I think Graphics.DrawImage(Image, Rectangle) with rectangle size equals to source image size will fix your problem.
The Image.Horizontal/VerticalResolution property matters. If it doesn't match the dots-per-inch setting of your monitor then the image is going to be drawn proportionally larger or smaller. This tends to be undesirable, use the DragImage(Image, Rectangle) overload to force it to display at exact 32 x 32 pixels.

Make overlapping picturebox transparent in C#.net

I have two overlapping pictureboxes.The images of both picture boxes have some transparent pixels.I want to see the bottom picture box through the transparent pixels of the overlapping picture box.
I tried setting the background color of both picture boxes as transparent.But it just sets the back color of the picture box to the background color of the form.
Clearly you are using Winforms. Yes, transparency is simulated by drawing the pixels of the Parent. Which is the form, you only see the form pixels, stacking effects don't work. There's a KB article that shows a workaround for this. It is painful. Another approach is to not use PictureBox controls but just draw the images in the form's Paint event.
Consider WPF, it has a very different rendering model that easily supports transparency.
Solutions to that problem might be various, and it mainly depends on your skills and amount of work will depend on kind of images you're dealing with. For example if images are always same resolution, size and overlapping image supports transparency you could try to do manipulation of two Image objects and draw one over another, then display it in PictureBox. Or if you will need to do it multiple times in various places of your app you could even consider creating your own UserContriol.
Code in answer of this question, method ResizeImagein particular, show how to create resized, good quality image, all you need it is to change it a little. Make it to get two Images as input parameters, and change it to draw one image over another.
Changes might look like this
public static Bitmap CombineAndResizeTwoImages(Image image1, Image image2, int width, int height)
{
//a holder for the result
Bitmap result = new Bitmap(width, height);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the images into the target bitmap
graphics.DrawImage(image1, 0, 0, result.Width, result.Height);
graphics.DrawImage(image2, 0, 0, result.Width, result.Height);
}
//return the resulting bitmap
return result;
}
And use it, for example, like this:
pictureBox1.Image = CombineAndResizeTwoImages(Image.FromFile("c:\\a.png"), Image.FromFile("c:\\b.png"), 100,100);
But that its only example, and you must tune it up to your needs.
Good luck.
If it's one PictureBox inside another, you can use:
innerPictureBox.SendToBack();
innerPictureBox.Parent = outerPictureBox;

C#: Why reduction of the size of an images leads to very bad quality of those images? Anything I can do

Few days ago, I asked a question about how to reduce the size of an image while keeping its ratio's dimensions. I was finally able to get it work. Now when the user uploads an image, 3 copies of it (with different dimensions) are saved to the database.
But, unfortunately, that dimensions' reduction degrades sensibly the quality of images uploaded. Only the copy that has been saved without dimensions has kept its quality.
Am I supposed to expect those reduction of quality? the image look really bad (like a news paper's photo).
Is there anything I can do? mighty an option I need to set up in my code.
Thanks for helping
The reason is because the Image.GetThumbnailImage function that you are using is only intended to make low quality images.
See these remarks from the MSDN documentation on GetThumbnailImage:
The GetThumbnailImage method works
well when the requested thumbnail
image has a size of about 120 x 120
pixels. If you request a large
thumbnail image (for example, 300 x
300) from an Image that has an
embedded thumbnail, there could be a
noticeable loss of quality in the
thumbnail image. It might be better to
scale the main image (instead of
scaling the embedded thumbnail) by
calling the DrawImage method.
For a good example of how to perform high quality image scaling in C# see this question:
High Quality Image Scaling C#
I'm not quite sure how the code you linked to works.
Here's a better example.
Resizing a Photographic image with GDI+ for .NET
The essence of it:
Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
GraphicsUnit.Pixel);
Look at the settings like InterpolationMode which allows you to control the quality (processing speed vs. quality).

How to render images in real-time

I am looking for a solution\software that I can use in order to render buttons in real time.
That is, I have an image and text, and I want them to be an image altogether in realtime.
Thank you
You can dynamically create an image based on text using the System.Drawing namespace.
private Bitmap CreateBitmapImage(string imageText, string imageUrl)
{
Bitmap myBitmap = new Bitmap(imageUrl);
Graphics g = Graphics.FromImage(myBitmap);
g.DrawString(imageText, new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));
return (objBmpImage);
}
Once you have the Bitmap object in memory you can save it to the disk and call it's location from the web.
Bitmap bmp = CreateBitmapImage("my picture", #"C:\myBasePic.bmp");
bmp.Save(#"C:\bla.png", System.Drawing.Imaging.ImageFormat.png);
It would be good if you can specify why such requirement is there. One of such scenario that I had encountered was need of image buttons with different text (round corners with shaded background) - this can easily be achieved using CSS. If you really want to combine an image and text together then you can do that at server side (using say System.Drawing types) and serve the combined image over a handler/web-service.

Categories