showing png files in a picturebox - c#

I am new to C# and i am willing to learn it by creating different projects (I am working at one right now). I am trying to make a picture box show different frames of a gif file. I have tried doing this with FrameDimension and SelectActiveFrame but it won't work. I have also tried to split the frames of the gif and separate them into different .png files. I have added them in my Resources folder in my project and i have tried to make the picture box show them.
Here is the code I wrote:
public void Form1
{
InitializeComponent();
DrawPNG();
}
public void DrawPNG()
{
Bitmap bmp = Properties.Resources.pic1;
picBox1.Image = bmp;
bmp = Properties.Resources.pic2;
picBox1.Image = bmp;
bmp = Properties.Resources.pic3;
picBox1.Image = bmp;
bmp = Properties.Resources.pic4;
picBox1.Image = bmp;
}
The problem is that the picture box will constantly show the first frame and it won't change. I am out of ideas of how to do this and I have looked up on the internet and haven't found anything. Can someone help me please?
Oh, and by the way, I am working on a Windows Forms Application.

Related

Displaying editable image in c#

I want to display an image (.tif, gray value 16 bit) which is editable for the user via sliders. The displayed image should directly react to the changes, so the user knows what he's doing to the image.
Currently I only seem to be creating new files with every change and displaying the latest one, which is a terrible solution.
My idea would be to load the original pixeldata, put it in some sort of temporary file which isn't and won't be saved, then saving the parameters of the sliders and when hitting save applying the parameters to the original image (since in the end the filter is used on an intire set of images).
An Image is not a File. If might be that you display the Image that is in a file, but once you've loaded the file into an Image object don't ever use the file again, until the edited image needs to be saved in a file.
You say you want to display an image. As you are using winforms, I assume you are using a PictureBox control. If not, you have a class that you order to display an Image.
Image imageToDisplay = GetImageToDisplay();
this.PictureBox1.Image = imageToDisplay;
GetImageTodisplay will read the file and return it as an Image
System.Drawing.Image GetImageToDisplay()
{
FileStream imageStream = new FileStream(this.GetImageFileName(), FileMode.Open);
System.Drawing.Image image = new Image(imageStream);
return image;
}
Apparently you also have sliders. You don't mention the class of your sliders, but apparently it is an object that notifies your form about a new slider value.
void OnSliderValueChanged(object sender, int sliderValue)
{
Image displayedImage = this.PictureBox1.Image;
this.ProcessSliderChanged(image, sliderValue);
}
Until now I don't see the creation of a new file. So this should be in ProcessSliderChange. Luckily that is a function you created yourself. All you have to do is change the image according to your new slider value. Don't do anything with file handling and you won't get a new file
void ProcessSliderChange(System.Drawing.Image image, int sliderValue)
{ // what do you want to do with the image?
// make it darker? move it? change contrast?
}
Unfortunately the Image class doesn't have a lot of functions to edit images, I assume you have a library with functions that act on System.Drawing.Image objects. Just use those functions to change your Image.

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.

Programmatically change jpg in picturebox

I have a simple application in winforms where I need to change the image depending on an if statement. The statement is triggering because other things are also happening. I've looked at the following examples:
Dynamically set Image of a Picturebox in winforms applications?
Change PictureBox's image to image from my resources?
Setting a picture from my Resources programmatically to a PictureBox
and none of these have led me to a solution to why I'm unable to change the image.
Here's what I have tried:
pictureBox1.Image = global::KatReminder.Properties.Resources.angry_orange_cat;
pictureBox1.Refresh();
pictureBox1.Load();
pictureBox1.Image = Image.FromFile(#"\Resources\angry-orange-cat.jpg");
pictureBox1.BackgroundImage = KatReminder.Properties.Resources.angry_orange_cat;
pictureBox1.Refresh();
pictureBox1.Load(#"\Resources\angry-orange-cat.jpg");
In the two examples with files, the full path I'm using has been truncated for this example.
You should try calling pictureBox1.Invalidate(). Usually that works for me when I need to make sure something gets repainted.

How to place a System.Drawing.Graphics object in browser window

I'd like to understand how to use C# for drawing an image in my browser window - alongside other HTML objects that are already there. (Idea: To place buttons and forms that allow the user to alter the image.)
I am doing this in Mono on Linux, but I'd like the thing to be portable.
What I came up with so far is:
public virtual void button1Clicked (object sender, EventArgs args)
{
int height = 300;
int width = 300;
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmp);
//
//Draw something on g here
//
Response.ContentType = "image/gif";
bmp.Save(Response.OutputStream, ImageFormat.Gif);
g.Dispose();
bmp.Dispose;
}
This clears the browser window and then displays the image, which is not what I wanted.
After extensive search I found the following tutorial,
http://www.dreamincode.net/forums/topic/67275-the-wonders-of-systemdrawinggraphics/
which says I should try to use a Handle on some control in my window, alternatively the CreateGraphics() method. Neither of these seem to exist for my "button1" or any other id of an entity in my .aspx page. Is that because I'm using Mono?
To sum up, what is the best way to draw inside a browser window in C# + asp.net??
i had the same thing to do some time ago,
the thing is that you will have to save your graphic object/bitmap to a location if you want to use it in an iFrame or Image control.
If you don't want to save it to a location,
try using a server script on your page :
http://www.developerfusion.com/article/2569/creating-images-on-the-fly-with-aspnet/

Capture a form to image

I have a form with controls, I need capture this form to image. Please help me. Thanks.
//Control cntrl; previously declared and populated
Bitmap bmp = new Bitmap(cntrl.Width,cntrl.Height);
cntrl.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
This works (almost) for me. I did note that MANY times the image would just be blank. When I searched online, it seems that many people were having this issue with the webBrowser control. It turns out that the initial image sometimes needs to be cleared to a solid color for this control. I really don't know why, but once it is initialized, I get much more consistent results. I've yet to get a blank image back.
Please note the code I've attached for clearing the initial image to all black.
try
{
string fn = Path.Combine(Path.GetTempPath(), "Error_screen.png");
Bitmap bmp = new Bitmap(internalBrowser.Width, internalBrowser.Height);
// In order to use DrawToBitmap, the image must have an INITIAL image.
// Not sure why. Perhaps it uses this initial image as a mask??? Dunno.
using (Graphics G = Graphics.FromImage(bmp))
{
G.Clear(Color.Black);
}
internalBrowser.DrawToBitmap(bmp, new Rectangle(0, 0,
internalBrowser.Width, internalBrowser.Height));
bmp.Save(fn, ImageFormat.Png);
}
catch
{
// handle exceptions here.
return "";
}
One VERY interesting side note (as compared to other solutions I've seen): This works for my control which is on a form that I have never shown! My form exists as a headless web browser, but this actual form has never seen the light of day.

Categories

Resources