I'm working on a program where I need to place an image on top of another image. What happening though is when I place the picture on top of the background it gets changed to a different resolution and I'm not sure why. I've tried messing around with the bit depth and DPI, but neither of them made any difference. My original image is 574x574, but when it places it on the picture, it becomes 768x768. Here is the code I'm using. Any help is appreciated.
Image imgBackground = Image.FromFile(r_strApplicationStartupPath + "\\images\\Backing.png");
Image imgPicture1 = Image.FromFile(r_strApplicationStartupPath + "\\images\\Picure1.png");
Image TempImg = Image.FromFile(r_strApplicationStartupPath + "\\images\\Backing.png");
Graphics grfx = Graphics.FromImage(TempImg);
Bitmap bmpFinal = new Bitmap(1296, 1944, PixelFormat.Format32bppArgb);
grfx = Graphics.FromImage(bmpFinal);
grfx.DrawImage(imgBackground, 0, 0);
grfx.DrawImage(imgPicture1, 659, 1282);
bmpFinal.Save(r_strApplicationStartupPath + "\\images\\" + r_strName + " Composite " + r_intCounter.ToString() + ".png", ImageFormat.Png);
When you call Graphics.DrawImage without specifying the destination rectangle, it assumes that you want to preserve the original physical size of the image (i.e. inches rather than pixels), so the image is resized based upon the DPI of the source image and the destination image.
If you want to ensure that the image gets drawn at the original pixel size without adjusting the DPIs then you just need to provide the entire destination rectangle.
grfx.DrawImage(imgPicture1, dstRect, srcRect, GraphicsUnit.Pixel);
Related
I'm writing a little program that tranfers a screen shot of a user screen to my wpf image control but when the image is displayed it is not full,not the whole screen is displayed even if the action was done through my computer and not my leptop and it is blurry
Bitmap bmp=
new Bitmap((int)SystemParameters.PrimaryScreenWidth(int)SystemParameters.PrimaryScreenHeight);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, bmp.Size);
the code above is the code to take the screenshot
BitmapImage bmI = new BitmapImage();
bmI.BeginInit();
bmI.UriSource = new Uri(FullPath);
bmI.EndInit();
Screen_Shot.Source = bmI;
And this code is the code to dislpay the image, Screen_Shot is the image name, and the full path is where i put the bitmap image
I tried to use
Screen_Shot.Stretch = Stretch.Fill; and UseLayoutRounding="True" SnapsToDevicePixels="True"
but none of them seems to get the job's done
this is an exsample of a screenshot i took from my own pc
You want to set the Stretch property to Uniform or else your image will be distorted as it will fill regardless of the Image control's size/shape. You also have to remember that the screen size may be different than your programs window size. If you don't uniformly stretch the image it will distort almost always (even if it's just a bit).
Regarding your "blurry" image, you need to specify the rendering option of you Screen_Shot image control or it will set the image to a lower quality that it can render more easily. This is honestly better to just set in the xaml. I normally use Fant, but look other options specified here.
<Image Name="Screen_Shot" RenderOption.BitmapScalingMode="Fant"/>
I'm using jQuery-cropbox to crop images on a web application.
Everything is running smoothly most of the time, but from time to time, the generated image is wrong, as if the cropping was applied with wrong coordinates.
As an example, consider this image :
In cropbox, I zoom and drag until I get this :
But when I click on the crop button, it gives me this:
Here is the cropping code:
private Bitmap CropImage(Image img, Rectangle rect)
{
Bitmap newImg = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage(newImg);
g.DrawImage(img, -rect.X, -rect.Y);
return newImg;
}
//...
// save image
var streamOut = new MemoryStream();
Bitmap newImg = CropImage(oldImg, rect);
newImg.Save(streamOut, ImageFormat.Png);
oldImg is the image previously posted, rect the coordinates I use to crop.
I checked the coordinates in rect of course, they are correct.
For reference, to obtain the result provided the coordinates are (those may not be exactly accurate, I took them from a second test, so I certainly placed the picture a bit differently):
x: 47
y: 94
w: 150
h: 150
What is happening ?
Your image (as posted) has 72dpi; your screen and therefore the Graphics you use probably have 96dpi.
This will lead to the effect you see.
To avoid the effect simply set the resolution of img before drawing it to the one in your Graphics object:
img.SetResolution((int)g.DpiX, (int)g.DpiY);
I want to make very simplistic paint/image editor. Mainly, for pixel editing, but that doesn't seem relevant.
To ease up my effort, I decided to keep the image size at 16x16.
I populate the form, add a PixelBox and slap a default image on it.
Of course, I need to make the pixels visible, set the interpolation to NearestNeighbor.
Then, I stretch the pixelbox to 320x320. And there the situation arises.
The image is displayed as thus:
Cropped image
Could someone shed some light on this? This is just a 16x16 image with a checkerboard pattern that I made, but I can't figure out why it is displayed with that offset at the top left.
Also, no code as been yet added. I assume this is default behavior?
If you look at the examples on the page that exact same error happens, so it must be a bug on the PixelBox.
Instead of using a custom control for this type of operation just use the standard PictureBox and scale the image by yourself:
public Bitmap ScaleBitmap(Bitmap src, Size NewSize)
{
Bitmap bmp = new Bitmap(NewSize.Width, NewSize.Height, src.PixelFormat);
Graphics g = Graphics.FromImage(src);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(src, new Rectangle(Point.Empty, NewSize), new Rectangle(0, 0, src.Width, src.Height), GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}
suppose i have two images img1.jpg and img2.jpg. using some routine i could extract the difference between two images. now difference is saved in another bitmap variable called diff
here is code
Bitmap diff = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(diff);
g.DrawImage(secondImg, 0, 0, bounds, GraphicsUnit.Pixel);
g.Dispose();
i know the difference in terms of rectangle and also save the difference in diff variable. now i want to merge or draw this difference on my first image. i tried with code like
Graphics g1 = Graphics.FromImage(firstImg);
g1.DrawImage(secondImg, 0, 0, bounds, GraphicsUnit.Pixel);
g1.Dispose();
but it is not working because when i open my first image img1.jpg then i am seeing any changes in that image. i want to draw the changes on my first image img1.jpg. what is wrong in my code which is not being able to dump or draw the changes on first image.
basically i have to reconstruct img1, if i have img2 and difference between img2 and img1.
please guide me. thanks
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.