Errors in the image resize class Visual c# - c#

I'm trying to make a puzzle game in microsoft visual c# 2010 and when I try to resize the image to fit the groupbox I get these errors:
error CS1502: The best overloaded method match for
'System.Drawing.Graphics.DrawImage(System.Drawing.Image,
System.Drawing.PointF)' has some invalid arguments
error CS1503: Argument 1: cannot convert from 'PuzzleImage.Form1' to
'System.Drawing.Image'
error CS1503: Argument 2: cannot convert from
'System.Drawing.Rectangle' to 'System.Drawing.PointF'
NOTE: The errors are in the 2nd part of the code, int the private Bitmap CreateBitmapImage(Form1 image) class.
Here is my code:
OpenFileDialog openFileDialog = null;
Form1 image;
PictureBox picBoxWhole = null;
private void buttonImageBrowse_Click(object sender, EventArgs e)
{
if (openFileDialog == null)
openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.Drawing.Image image = new Bitmap(openFileDialog.FileName);
if(picBoxWhole== null)
{
picBoxWhole= new PictureBox();
picBoxWhole.Height = groupboxPuzzle.Height;
picBoxWhole.Width =groupboxPuzzle.Width;
groupboxPuzzle.Controls.Add(picBoxWhole);
}
picBoxWhole.Image= image;
}
}
private Bitmap CreateBitmapImage(Form1 image)
{
Bitmap objBmpImage = new Bitmap(groupboxPuzzle.Width, groupboxPuzzle.Height);
Graphics objGraphics = Graphics.FromImage(objBmpImage);
objGraphics.Clear(Color.White);
int x = groupboxPuzzle.Width;
int y = groupboxPuzzle.Height;
objGraphics.DrawImage(image, new Rectangle(0,0, x, y));
objGraphics.Flush();
return objBmpImage;
}
And here is the tutorial that I am currently following. Also someone said something about an error at 77 step too.

Why are you transferring a Form parameter in your CreateBitmapImage() function? It says in that tutorial to accept Image type parameter.
private Bitmap CreateBitmapImage(Image image)

you need to change
Form1 image; to Image image; //only if you need this :)
and
private Bitmap CreateBitmapImage(Form1 image) to private Bitmap CreateBitmapImage(Image image)

Related

How to check if image object is the same as one from resource?

So I'm trying to create a simple program that just change the picture in a picture box when it's clicked. I'm using just two pictures at the moment so my code for the picture box click event function
looks like that:
private void pictureBox1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == Labirint.Properties.Resources.first)
pictureBox1.Image = Labirint.Properties.Resources.reitmi;
else if (pictureBox1.Image == Labirint.Properties.Resources.reitmi)
pictureBox1.Image = Labirint.Properties.Resources.first;
}
For some reason the if statement it's not working and the picture don't change.
What should I do?
Note: original code contained bug with second if undoing effect of first if condition would work with fix suggested by Cyral's answer, but adding else did not fix the issue - stepping through the code with else still shows no matches for either image.
if (pictureBox1.Image == Labirint.Properties.Resources.first)
pictureBox1.Image = Labirint.Properties.Resources.reitmi;
if (pictureBox1.Image == Labirint.Properties.Resources.reitmi) // was missing else
pictureBox1.Image = Labirint.Properties.Resources.first;
if (pictureBox1.Image == Labirint.Properties.Resources.first)
There's a trap here that not enough .NET programmers are aware of. Responsible for a lot of programs that run with bloated memory footprints. Using the Labirint.Properties.Resources.xxxx property creates a new image object, it will never match any other image. You need to use the property only once, store the images in a field of your class. Roughly:
private Image first;
private Image reitmi;
public Form1() {
InitializeComponent();
first = Labirint.Properties.Resources.first;
reitmi = Labirint.Properties.Resources.reitmi;
pictureBox1.Image = first;
}
And now you can compare them:
private void pictureBox1_Click(object sender, EventArgs e) {
if (pictureBox1.Image == first) pictureBox1.Image = reitmi;
else pictureBox1.Image = first;
}
And to avoid the memory bloat:
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
first.Dispose();
reitmi.Dispose();
}
You need to use else if, because if the image is first, you set it to reitmi, then you check if it is reitmi, which it now is, and change it back to first. This ends up not appearing to change at all.
if (pictureBox1.Image == Labirint.Properties.Resources.first)
pictureBox1.Image = Labirint.Properties.Resources.reitmi;
else if (pictureBox1.Image == Labirint.Properties.Resources.reitmi)
pictureBox1.Image = Labirint.Properties.Resources.first;
Maybe this code can be a bit large, but works just fine with me, try it:
This is the requeriment:
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Collections;
And here is the code to compare:
// Your Images
Image img1 = pictureBox1.Image;
Image img2 = pictureBox2.Image;
// Now set as bitmap
Bitmap bmp1 = new Bitmap(img1);
Bitmap bmp2 = new Bitmap(img2);
// here will be stored the bitmap data
byte[] byt1 = null;
byte[] byt2 = null;
// Get data of bmp1
var bitmapData = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp1.PixelFormat);
var length = bitmapData.Stride * bitmapData.Height;
//
byt1 = new byte[length];
//
Marshal.Copy(bitmapData.Scan0, byt1, 0, length);
bmp1.UnlockBits(bitmapData);
// Get data of bmp2
var bitmapData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp2.PixelFormat);
var length2 = bitmapData2.Stride * bitmapData2.Height;
//
byt2 = new byte[length2];
//
Marshal.Copy(bitmapData2.Scan0, byt2, 0, length2);
bmp2.UnlockBits(bitmapData2);
// And now compares these arrays
if (StructuralComparisons.StructuralEqualityComparer.Equals(byt1, byt2))
{
MessageBox.Show("Is Equal");
}
else
{
MessageBox.Show("Isn`t equal");
}
this code compares each byte image to generate the result. may have other easier ways.

I can't get my IF statement to fire with Label.BackgroundImage == Properties.Resources.image1 [duplicate]

So I'm trying to create a simple program that just change the picture in a picture box when it's clicked. I'm using just two pictures at the moment so my code for the picture box click event function
looks like that:
private void pictureBox1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image == Labirint.Properties.Resources.first)
pictureBox1.Image = Labirint.Properties.Resources.reitmi;
else if (pictureBox1.Image == Labirint.Properties.Resources.reitmi)
pictureBox1.Image = Labirint.Properties.Resources.first;
}
For some reason the if statement it's not working and the picture don't change.
What should I do?
Note: original code contained bug with second if undoing effect of first if condition would work with fix suggested by Cyral's answer, but adding else did not fix the issue - stepping through the code with else still shows no matches for either image.
if (pictureBox1.Image == Labirint.Properties.Resources.first)
pictureBox1.Image = Labirint.Properties.Resources.reitmi;
if (pictureBox1.Image == Labirint.Properties.Resources.reitmi) // was missing else
pictureBox1.Image = Labirint.Properties.Resources.first;
if (pictureBox1.Image == Labirint.Properties.Resources.first)
There's a trap here that not enough .NET programmers are aware of. Responsible for a lot of programs that run with bloated memory footprints. Using the Labirint.Properties.Resources.xxxx property creates a new image object, it will never match any other image. You need to use the property only once, store the images in a field of your class. Roughly:
private Image first;
private Image reitmi;
public Form1() {
InitializeComponent();
first = Labirint.Properties.Resources.first;
reitmi = Labirint.Properties.Resources.reitmi;
pictureBox1.Image = first;
}
And now you can compare them:
private void pictureBox1_Click(object sender, EventArgs e) {
if (pictureBox1.Image == first) pictureBox1.Image = reitmi;
else pictureBox1.Image = first;
}
And to avoid the memory bloat:
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
first.Dispose();
reitmi.Dispose();
}
You need to use else if, because if the image is first, you set it to reitmi, then you check if it is reitmi, which it now is, and change it back to first. This ends up not appearing to change at all.
if (pictureBox1.Image == Labirint.Properties.Resources.first)
pictureBox1.Image = Labirint.Properties.Resources.reitmi;
else if (pictureBox1.Image == Labirint.Properties.Resources.reitmi)
pictureBox1.Image = Labirint.Properties.Resources.first;
Maybe this code can be a bit large, but works just fine with me, try it:
This is the requeriment:
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Collections;
And here is the code to compare:
// Your Images
Image img1 = pictureBox1.Image;
Image img2 = pictureBox2.Image;
// Now set as bitmap
Bitmap bmp1 = new Bitmap(img1);
Bitmap bmp2 = new Bitmap(img2);
// here will be stored the bitmap data
byte[] byt1 = null;
byte[] byt2 = null;
// Get data of bmp1
var bitmapData = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp1.PixelFormat);
var length = bitmapData.Stride * bitmapData.Height;
//
byt1 = new byte[length];
//
Marshal.Copy(bitmapData.Scan0, byt1, 0, length);
bmp1.UnlockBits(bitmapData);
// Get data of bmp2
var bitmapData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp2.PixelFormat);
var length2 = bitmapData2.Stride * bitmapData2.Height;
//
byt2 = new byte[length2];
//
Marshal.Copy(bitmapData2.Scan0, byt2, 0, length2);
bmp2.UnlockBits(bitmapData2);
// And now compares these arrays
if (StructuralComparisons.StructuralEqualityComparer.Equals(byt1, byt2))
{
MessageBox.Show("Is Equal");
}
else
{
MessageBox.Show("Isn`t equal");
}
this code compares each byte image to generate the result. may have other easier ways.

graphics from a class onto a form

Okay, so I needed to make a simple animation in c# to use as a loading icon. This worked all fine and good so lets take this square as an example
PictureBox square = new PictureBox();
Bitmap bm = new Bitmap(square.Width, square.Height);
Graphics baseImage = Graphics.FromImage(bm);
baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100);
square.Image = bm;
So with that I made my animation and everything here worked, but then I realized that I need my animation to be in a class so I can call it from my co workers programs to use the animation. This is where the problem came in, I made my class and I did everything the same way but in a class instead of the form I then called my class from my form but the screen was blank and there was no animation. Is there something that needs to be passed in order to do this?
namespace SpinningLogo
{//Here is the sample of my class
class test
{
public void square()
{
PictureBox square = new PictureBox();
Bitmap bm = new Bitmap(square.Width, square.Height);
Graphics baseImage = Graphics.FromImage(bm);
baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100);
square.Image = bm;
}
}
}
private void button1_Click(object sender, EventArgs e)
{//Here is how I call my class
Debug.WriteLine("11");
test square = new test();
square.square();
}
Pass your test class a reference to the PictureBox that is on the form:
namespace SpinningLogo
{
class test
{
public void square(PictureBox thePB)
{
Bitmap bm = new Bitmap(thePB.Width, thePB.Height);
Graphics baseImage = Graphics.FromImage(bm);
baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100);
thePB.Image = bm;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
test square = new test();
square.square(myPictureBox); //whatever the PictureBox is really named
}
You could also pass the Form itself (using this), but then you'd still have to ID the PictureBox control (I assume).
You should pass to your test class Form instance, and not define PictureBox in test class. PictureBox should be field of Form, and by Form instance u will get access to your PictureBox.

Add Image in a stackpanel based on textbox input

I'm trying to add Image stored in ..\Resources\ebi.png in a stackpanel. Most of the times, the same image will be displayed in Stackpanel, depending on Textbox input "EtReqCount". Below is the sample code tried but getting error saying
"Specified Visual is already a child of another Visual or the root of a CompositionTarget"
Below is the code tried:
private BitmapImage bmp = new BitmapImage(new Uri("WpfApplication1;component/Resources/ebi.png", UriKind.RelativeOrAbsolute));
private void EtReqCount_TextChanged(object sender, TextChangedEventArgs e)
{
StackPanel dynamicStackPanel = new StackPanel();
dynamicStackPanel.Width = 300;
dynamicStackPanel.Height = 200;
dynamicStackPanel.Background = new SolidColorBrush(Colors.LightBlue);
dynamicStackPanel.Orientation = Orientation.Vertical;
if (EtReqCount.Text != "")
{
for (int k = 1; k <= Int32.Parse(EtReqCount.Text); k++)
{
Image img = new System.Windows.Controls.Image(); // This makes the difference.
img.Source = bmp;
dynamicStackPanel.Children.Add(img);
}
}
}
XAML Code:
It is apparent. The same image is already a child of the StackPanel, you cannot add it again and again.
Moreover, you should use a private memeber to hold the repetitively used bmp to save resource:
outside the class method, and inside class.cs:
private BitmapImage bmp=new BitmapImage(new Uri("/....../ebi.png", UriKind.RelativeOrAbsolute);
You can create new instances of the image:
for(int i=0; i<...;i++)
{
img = new System.Windows.Controls.Image(); // This makes the difference.
img.Source = bmp;
dynamicStackPanel.Children.Add(img);
}

Image Zoomer tool in c#

I am trying to make a trackbar which will zoom in and out on a picture in a picturebox. This is my current code:
namespace Zoom_in_and_Out_Tool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Image imgOriginal;
private void Form1_Load(object sender, EventArgs e)
{
// set image location
imgOriginal = Image.FromFile(#"C:\New Folder\picture1.jpg");
picBox.Image = imgOriginal;
// set Picture Box Attributes
picBox.BackgroundImageLayout = ImageLayout.Stretch;
// set Slider Attributes
zoomSlider.Minimum = 1;
zoomSlider.Maximum = 5;
zoomSlider.SmallChange = 1;
zoomSlider.LargeChange = 1;
zoomSlider.UseWaitCursor = false;
// reduce flickering
this.DoubleBuffered = true;
}
public Image PictureBoxZoom(Image img, Size size)
{
Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
Graphics grap = Graphics.FromImage(bm);
grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
return bm;
}
private void zoomSlider_Scroll(object sender, EventArgs e)
{
if (zoomSlider.Value > 0)
{
picBox.Image = null;
picBox.Image = PictureBoxZoom(imgOriginal, new Size(zoomSlider.Value, zoomSlider.Value));
}
}
}
}
Currently it comes up with 2 problems. One being it does want to compile with the line grap.InterpolationMode = InterpolationMode.HighQualityBicubic; . The Second problem is that when i try to zoom it comes up with the error: " "ArgumentException was unhandled" error at the line: Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height)); " Any help would be great,
Thanks
UPDATE
The first error says: "The name 'InterpolationMode' does not exist in the current context"
The second error when i comment this line out is: 'NullReferenceException was unhandled "Object reference not set to an instance of an object.' on the line Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
Thanks
Include
using System.Drawing.Drawing2D;
in your using list.
The second error could be due to either the img being null or the size being null.
The first compiler error is more than likely caused by an unknown reference to InterpolationMode.HighQualityBicubic. The InterpolationMode enumeration is found in the Drawing2D namespace, which is a child namespace of System.Drawing.
You can fix this error either by adding an additional Using directive for System.Drawing.Drawing2D, or by fully qualifying the namespace in your code:
grap.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic;
The second problem with your code is that the image you're specifying as a parameter to this method (img) is a null reference. The Scroll event of your zoom slider is probably getting raised as soon as the control is created (in your form's constructor), which is before the code in your form's Load method is run, which is what creates the image (by loading it from a file on disk).
Try adding a null check to the Scroll event handler:
private void zoomSlider_Scroll(object sender, EventArgs e)
{
if ((zoomSlider.Value > 0) && (imgOriginal != null))
{
picBox.Image = null;
picBox.Image = PictureBoxZoom(imgOriginal, new Size(zoomSlider.Value, zoomSlider.Value));
}
}
Finally, I noticed that you're setting the BackgroundImageLayout property of the picture box, but none of the code you post is actually specifying a background image for the picture box. Did you mean to set the SizeMode property to adjust how the image is displayed? Something like:
picBox.SizeMode = PictureBoxSizeMode.StretchImage;

Categories

Resources