Windows Develop Bookmark and Share   
 index > Windows Forms General > Image Stretch -- No Gradient
 

Image Stretch -- No Gradient

Hello:

I have an image that is 1 x 1.

Image img = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(img);
g.FillRectangle(Brushes.Black, 0, 0, img.Width, img.Height);
g.Dispose();

Well, as you can see this image is just a black box.  Well, I have another graphics device that I'm trying to call DrawImage().
I want to have the black box fill a specific porition of the graphics device.

EXA:

g.DrawImage(img, new Rectangle(x, y, w, h), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);

You can see that the image is going to be stretched, but when it stretches pretty far, it is performing a gradient; it starts from black, moves to gray, and eventually fades to white if the distance is large.

I know I could just perform a grfx.FillRectangle(), but this NEEDS to be an image (long story).  Also, the variables (x, y, w, h) are not known until runtime, so I can't just hard code the dimensions, and I don't want to create a HUGE bitmap and compress it.  As such, I ask, "Is it possible to have the black expand without the gradient effect using my 1 x 1 black image?"

Thank you.


Trecius
Trecius  Tuesday, October 06, 2009 6:28 PM
Just add this property assignment before the DrawImage() call:

            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;


Hans Passant.
  • Marked As Answer byTrecius Wednesday, October 07, 2009 10:47 PM
  •  
nobugz  Tuesday, October 06, 2009 6:47 PM
Before drawing first image to second resize the image.

Here is a sample.

Image img = new Bitmap(1, 1);
            Graphics g = Graphics.FromImage(img);
            g.FillRectangle(Brushes.Black, 0, 0, img.Width, img.Height);
            g.Dispose();

            int x = 10;
            int y = 10;
            int w = 60;
            int h = 60;

            Image img2 = new Bitmap(100, 100);
            Graphics g2 = Graphics.FromImage(img2);
            g2.DrawImage(img.GetThumbnailImage(w,h,null,IntPtr.Zero), x, y, w, h);
Tamer Oz  Tuesday, October 06, 2009 6:38 PM
Just add this property assignment before the DrawImage() call:

            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;


Hans Passant.
  • Marked As Answer byTrecius Wednesday, October 07, 2009 10:47 PM
  •  
nobugz  Tuesday, October 06, 2009 6:47 PM
Nobugz addressed your question, but why wouldn't Graphics.Clear work?
JohnWein  Wednesday, October 07, 2009 12:29 AM

You can use google to search for other answers

Custom Search

More Threads

• Windows Application : Form resizing is very slow
• richtextbox
• Timer Residual WM_TIMER
• Manipulting size of Rectangle created through GDI+ on mouse events like MouseDrag.
• Passing a Parameter in a DataGrid ButtonColumn
• Keypressed question
• How to get FileName of image present in picturebox
• Circular Drag Operation
• Wait for Form to close...
• How to set cutom paper to printer from my program in Windows 98?