Images Scaling Down in draw in C# - c#

Alright guys last little bit of this project I'll ask for help on I promise.
So I go to load the images, works fine however I notice upon loading that the dimensions of the image have been scaled down in the y to 300 (all are a constant value of 433) and up or down from their original width to 600.
I'm using the following method to load them
foreach (string file in Directory.EnumerateFiles(imagePath, "*.JPG"))
{
Image contents = Image.FromFile(file);
treesImage[count] = contents;
count++;
}
and this is the resulting image when I have it loaded.
http://i.stack.imgur.com/Q40kK.png
As you can see the image below the red rectangle is quite small
Any help would be appreciated. If you require any more information please post below and I'll make sure to edit the original question with the relevant information as soon as humanly possible.
EDIT: I am using a simple windows form application and not another graphical framework for my own reasons.
Thanks in advance :)

I'll assume you are using a PictureBox control to display the image.
When someone chooses a tree from your map, you obviously set the PictureBox Image property to the image object referenced by the index in the array. Use the Image object to set the ClientSize of the PictureBox control.
...
Image img = treesImage[idx];
MyPictureBox.SizeMode = PictureBoxSizeMode.Normal;
MyPictureBox.ClientSize = new Size(img.Width,img.Height);
MyPictureBox.Image = img;
...
Alternately you can define one size for your PictureBox and force all the images to be scaled to that size by setting the control SizeMode property to StretchImage declaratively.
I would recommend that you create a simple class (MyImageInfo for example) that would store the Path, Width, and Height of the images found in your first function into a list and then just as before when a user clicks to view an image you set the width and height of the PictureBox and then call the LoadAsync(path) method to get the image. then you aren't storing all images in memory at once, just as you need them since it doesn't look like this requires a lot of quick jumping from image to image.

Related

How to display pixels 1:1 in a PictureBox

I have a bitmap (from a .bmp file) that is actually a small part of a larger screen dump that I took earlier.
Now I want to display that bitmap in a PictureBox control (WinForms), but I have trouble getting it to display at the same ratio as when it was captured originally.
I assume it has something to do with Windows' DPI setting for my monitor, but how can I shortcut that and make the bitmap be displayed exactly as when captured?
I figured it out! The problem was not WinForms, but the horizontal and vertical resolution associated with the displayed bitmap. When copying the part of the original screendump into a new bitmap (the one that is later assigned to the Image property of the PictureBox), the new bitmap had a different (=default) resolution setting. By changing the resolution setting to match the one from the source bitmap (the original screendump), the problem went away!

C# white or black unwanted pixels appear in the image

I am new to C#, I am using Visual Studo 2010, I have an image that needs to be displayed on a picturebox.
I tried many different methods. All the methods results in some unwanted pixels to appear along with image.
I have tried
picturebox.Image = Image.FromFile("bird.png");
result->
the image is displayed but with the stray white/black pixels at random places.
I also tried creating a bitmap of same size and drawing the image onto the bitmap and then assigning the bitmap to the picture box.Image. Still those unwanted pixels are visible.
I have tried clearing the picture box image (filling it with white or transparent) and then assigning the image, still same error occurs.
PS: this doesn't happen for all the images only certain images show this behaviour.
any help would be great
Code:
Image org = Bitmap.FromFile("bird.png");
Bitmap final = new Bitmap(org.Width,org.Height);
using (Graphics g = Graphics.FromImage(final))
{
g.DrawImage(org,0,0,GraphicsUnit.Pixel);
}
picturebox.Image = final;
if i use final.save("picture.png"). The "picuture.png" does not have that wrong pixels, it happens only when i use picture box to display it.
Please find the images attached defect orginal
PS: its not because of different fileformat (orginal and defect)
It was an TransperancyKey issue resolved by setting it to Default.

Stopping effects from repeating on one another

I posted this question earlier but I have modified my code to a simple algorithm and I still have the same issue as before:
I created a picturebox which, when an effect is selected, it will then change the image in the picturebox using the color matrix.
The issues I'm having is if I choose another effect when one is selected the old effect will not disappear, instead it will just stay there and be underneath the new effect selected. The effects I'm using is sepia and greyscale for now, but can anyone help me so that once one effect is selected, the old effect is cleared rather than them just stacking up on one another. "
The key here is to cache the originalImage in a non-volatile area. For example, load it up into a hidden pictureBox that you don't touch.
When you want to apply an effect, copy the originalImage over into a displayImage picturebox and then apply the effect.
It also looks like you are setting your image to the image that is there. I am not sure but this may be taking into account what you have already set. For example when you set
Image originalImage = pictureBox.Image;
This may be taking the image you have displayed along with any effects you have already applied and setting that as your image to be modified. Like I said I am unsure of this since I can't test it at the moment.
EDIT
The following works for me:
Replace
Bitmap originalImage = (Bitmap)displayPictureBox.Image;
originalImage = (Bitmap)pictureBox.Image.Clone();
With
Image therealoriginalimage = Image.FromFile(#"C:\Users\Me\Desktop\testimg.png");
Bitmap originalImage = (Bitmap)therealoriginalimage;
As both answers mention it looks like you are setting your original image = what is currently in the picture box.
The above code is a quick fix so you can see exactly what was happening. You should modify this and have the originalimage saved as a varible as soon as your app starts. It will be much cleaner than the example above that sets the image each time you call the method

How to create an image with multiple tiles in the background?

I would like to know how I can create one image from many. I would like to create a tile in my windows phone application like in this image (specifically, the People tile):
(source: addictivetips.com)
I have nine pictures, and I would create an image, that I will add like tile to background. Does anybody know how can I create an image that looks like the one in that picture?
I have very little experience in this space, but have you considered creating a control that simply displays up to 9 pictures side by side in a grid like that? You then can bind each image independently & change them out however you want. This article touches on how to bind phontos in WP7 nicely:
http://msdn.microsoft.com/en-us/library/hh286418(v=vs.92).aspx
If you're talking about assembling an actual graphic image like a jpeg or bitmap, you'll need to look at the Image Class, Bitmap Class, and Graphics Class. Essentially you'll need to implement the following steps:
Load the relevant images with From method in Image, typically Image.FromFile.
Determine how many rows and columns you'll be using.
Calculate the total width and height for your layout using the widths and heights of the loaded images with appropriate padding added.
Create a new Bitmap of the appropriate size with the correct background color and iamge format.
Have variables for the current drawing location (x & y).
Have variables for the current row and column in your layout.
In a loop, Create your Graphics object.
Use Graphics.DrawImage to add your loaded image to the layout bitmap.
Increment your drawing row and or column as appropriate.
Calculate your new drawing location.
Repeat until done.
One of the options is to use WriteableBitmapEx
Also you can probably find an answer to your question here: How can I merge two images into one?

Draw a bitmap from a control taller than the screen

I am having quite a few problems saving a C# control to a bitmap file. The control in question is a kind of drawing surface on which the user can write text, draw pictures and paint boxes. The control is resizable and draggable. What happen is, when the control is really big and is not totally visible on the screen, the parts of the control not visible are saved half-drawn on the bitmap. The code used to generate the bitmap is quite simple:
Bitmap bitmap = new Bitmap(myControl.Width, myControl.Height);
myControl.DrawToBitmap(bitmap);
I have tried the following methods to try to have a fully painted bitmap, without any success:
myControl.Invalidate(myControl.ClientRectangle, true);
myControl.Refresh();
myControl.Update();
Application.DoEvents();
I cannot scale the control down to make it fully visible since resolution and image quality are very important for that project. In fact, I am actually trying to scale the image up to increase it's quality. Are there ways I am not aware of generating an image from a control ?
Tank you.
DrawToBitmap has limitations and dont always work as expected. Try instead work with native GDI+
Here is example
Maybe my answer here Capturing a Window that is hidden or minimized can help you?

Categories

Resources