1
Answer

PictureBox and antialiasing (smoothing)

jgress

jgress

21y
6.2k
1
I'm a newbie and I'm trying to do something that I know is simple. I'm using a picturebox to display a tif image and the following line loads the image I want displayed (I know I'm using the wrong terminology when I say "load"). pictureBox1.Image = Image.FromFile(ImageName); The image is there and I've stretched it using pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; So far so good, but the image doesn't look too good. I want to use antialiasing and maybe pixel offsets to clean it up a little and this is where I run into trouble. I'm not sure how to use these properties or call methods from another class. I'm not sure if I should even be using OnPaint or something else. I tried overriding the OnPaint method like below and added a MessageBox statement there to see when it fires and it only seems to fire when the program begins to run, not later after I've instantiated a picturebox object and told it which image I want displayed. How do I use the following, or whichever method is correct, so that it does the antialiasing and renders this in the picturebox? I know this can't be too hard. I'm hoping it's not. protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; Pen pen = new Pen(ForeColor); grfx.SmoothingMode = SmoothingMode.HighQuality; grfx.PixelOffsetMode = PixelOffsetMode.HighQuality; } Last night, after getting ready to post this, I tried one more thing, thinking that I can figure this out myself. Graphics grfx = Graphics.FromImage(pictureBox1.Image); grfx.SmoothingMode = SmoothingMode.HighQuality; grfx.PixelOffsetMode = PixelOffsetMode.HighQuality; grfx.TextRenderingHint = TextRenderingHint.AntiAlias; For the TIFF image I was trying to view I got the following exception: "A graphics object cannot be created from an image that has an indexed pixel format". When viewing a JPG I didn't get the error message, but the image looked no different with the graphics properties set. Now I have two problems; not only do I not know how to set these options so that the image is viewed in the picturebox using the graphics properties, but even if I can figure it out it doesn't seem to work with TIFF images and that's all I'm using. Thanks in advance, Jeff

Answers (1)