Using custom image buttons to display an image in RichTextBox - c#

I am creating a text editor much like notepad/word and i have buttons on my form with custom images attached. The images have been loaded into the resource folder. Now when i click the button i want it to display that same image in the RichTextBox, the images being displayed will be various emoticons. I only found ways to create picture viewers and not this.
private void button11_Click(object sender, EventArgs e)
{
Image.FromFile("../../Resources/sad-icon.png");
}
This will be used to place a sad emoticon in the RichTextBox. This is what i have so far i am pretty new to C#.

I have been trying various methods and i found one that works but only with certain emoticons and not the emoticons that i need to be able to use.
void InsertEmoticon(string ImageName)
{
int StringLength = 0;
StringLength = txtPAD.Text.Length;
Image img = Image.FromFile("../../Resources/Emoticons/" + ImageName + ".png");
Clipboard.SetImage(img);
txtPAD.SelectionStart = (StringLength + 1);
txtPAD.Paste();
txtPAD.Clear();
}
I will then use:
private void button10_Click(object sender, EventArgs e)
{
InsertEmoticon("glad");
}
Am i missing something here? The images have been loaded into the resource folder and are inside the folder Emoticons.

Related

How do I control the clicks of a browse button?

I have a browse button, and two empty images (image1, image2). I want to first click the browse button and load an image to (image1). On the second click I want to load the image to (image2).
I'm very new to WPF and C# in general. I guess I need some method to control the clicks of the button? Does anyone have any idea about this? I would highly appreciate it.
This is my code behind attempt:
private void button1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.DefaultExt = ".tif";
fd.Filter = "(*.tif,*.tiff)|*.tif;*.tiff";
fd.ShowDialog();
string fname = fd.FileName;
textBox1.Text = fname;
image1.Source = new BitmapImage(new Uri(fd.FileName));
}
After this, the first image is displayed in image1, but when I browse for another image it comes on top of image 1, and not in image2. How can I make the second image that I browse display in image2? maybe something like, if the button is already clicked one time, then the image should load into image2? or if image1 is already full then load to image2? I'm not sure!
Oh and the purpose of this is to create an interface that lets the user browse different images shown in a listbox, and when the user clicks each image, it displays in another window and the user can zoom in and out and so on.
But right now I'm just stuck with this small part of my project!
While I question the why you want to do such a thing, you could use the following. Also, please show some effort next time. This is a relatively easy solution!
private bool _ImageOneSet;
public MainWindow()
{
InitializeComponent();
_ImageOneSet = false;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!_ImageOneSet)
{
// set image one
_ImageOneSet = true;
}
else
{
// set image two
}
}
I use a private field that is set to false. The first time the button_click event gets triggered, _ImageOneSet is still false, so you can set the first image.
The second (and third, fourth etc...), _ImageOneSet is true so you will set the second image.

Previewing images in a listBox using pictureBox

I am using Winforms to create a 2D Map Editor.
I want to be able to preview an image of my assets that are stored in listBox using a pictureBox.
My current code for doing so is thus.
private void listBox_Assets_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(((FileInfo)listBox_Assets.SelectedItem).FullName);
}
But when I select an asset I get this error.
Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.
I have searched high and low for a solution but can't find an answer to this error, any help would be greatly appreciated.
You use the file name from the listbox like this, and protect the code with a check for the file.
private void listBox_Assets_SelectedIndexChanged(object sender, EventArgs e)
{
string file = IO.Path.Combine("the directory", listBox_Assets.SelectedItem);
if (IO.File.Exists(file))
pictureBox1.Image = Image.FromFile(file);
}

How can I make a scrollabe image list in C# Windows Form?

I need to create thumbnails list of pictures located on web.
I would like also to add CheckBox to make thumbnails choose able.
I'm trying to load pictures from urls to ListBox
// from form design file:
System.Windows.Forms.ListBox listBoxPictures;
// from main file
foreach (Photo albumPhoto in album.Photos)
{
PictureBox albumsImg = new PictureBox();
albumsImg.LoadAsync(albumPhoto.URL); // URL is string
CheckBox selectedPhotoCheckBox = new CheckBox();
listBoxPictures.Items.Add(albumsImg);
listBoxPictures.Items.Add(selectedPhotoCheckBox);
}
It doesn't work, there are no images appear in ListBox.
What am I doing wrong?
How can I make a scrollabe image list in C# Windows Form?
what is wrong is that you have to wait load completed of picture
private void button1_Click(object sender, EventArgs e)
{
//make your loop here
pictureBox1.WaitOnLoad = false;
pictureBox1.LoadCompleted += new AsyncCompletedEventHandler(pictureBox1_LoadCompleted);
pictureBox1.LoadAsync(albumPhoto.URL);
}
void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
//now your picture is loaded, add to list view
CheckBox selectedPhotoCheckBox = new CheckBox();
listBoxPictures.Items.Add(albumsImg);
listBoxPictures.Items.Add(selectedPhotoCheckBox);
}

picture viewer in c# .net

Regarding .Net technology,What will be the basic element of a picture viewer(like Windows Picture and Fax Viewer)? Is it a user control inside a form, or is it something other components. Could you please give me an idea in the context of C#.Net
You don't really get one that's bundled into .NET Framework (and that's probably a good thing, it's fairly large already).
If using WinForms, the nearest thing you do get is the PictureBox component, and the BackgroundImage property of some other components like Form and Panel. But you have to implement the rest of the functionality of an image viewer yourself.
WPF certainly has its own equivalents but I can't name them off the top of my head.
for making picture viewer is better to have PictureBox to show Pictures, ImageList to hold list of images due to have more that one picture and also two buttons for next pictur and previews picture.
a simple code which I suggest is as follow:
private void btnLoadImage_Click(object sender, EventArgs e)
{
imageList1.Images.Clear();
if (openFDialogImage.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < openFDialogImage.FileNames.Length; i++)
{
imageList1.Images.Add(Image.FromFile(openFDialogImage.FileNames[i]));
}
pictureBox1.Image = imageList1.Images[currentIndex];
}
}
private void BtnForward_Click(object sender, EventArgs e)
{
if(currentIndex!=imageList1.Images.Count-1 && imageList1.Images.Count > 0)
{
pictureBox1.Image = imageList1.Images[currentIndex++];
}
}
private void btnBack_Click(object sender, EventArgs e)
{
if (currentIndex!=0)
{
pictureBox1.Image = imageList1.Images[--currentIndex];
}
}
Thats all, :)

How do I associate a button with a control?

OR - How To Shave A Koala To Stop It Looking Squashed. (But I didn't think that would make a suitably techy title)
The Problem: You have three preview images derived from a main image. The preview images are resized for standardised picture spaces on a company website, the main image can be any size image from anywhere.
Example: The main image is a hi-res image of a koala bear measuring 2000x2250. Your previews want to render the koala at 200x200, 200x50 and 250x150.
Your utility program resizes and stretches the original image to the size of your three "actual size" previews but obviously each preview looks a bit squashy and you know everyone hates to see a squashed koala.
To resolve this you add a little cropping method to your program which shaves five pixels from the preview on the desired side. This means you should be able to resize your image and unsquash your koala by shaving off the unnecessary parts of the image.
You add four buttons to each preview image picture box and create four generic methods for sending the correct shaving instructions to the crop method. You want to associate each specific button with a specific picturebox on the form, but you want to send all the click events to four generic functions.
How do you tell the generic function which of the three preview picturebox images you want it to shave in an elegant and wonderful way?
Example Code:
//cropPict=method for cropping the picture in the relevant picturebox.
//CropSide=a little enum which tells the method which side to crop.
private void btnT_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Top);
}
private void btnB_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Bottom);
}
private void btnR_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Right);
}
private void btnL_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Left);
}
EDIT: As it happens, inspired by Hans below, rather than just stuffing the PictureBox into the tag. Which was a great idea I actually put a KeyValuePair into the tag for each button like so:
btnCCB.Tag = new KeyValuePair<CropSide,PictureBox>(CropSide.Bottom,pbxKoala);
btnCCL.Tag = new KeyValuePair<CropSide, PictureBox>(CropSide.Left, pbxKoala);
btnCCR.Tag = new KeyValuePair<CropSide, PictureBox>(CropSide.Right, pbxKoala);
btnCCT.Tag = new KeyValuePair<CropSide, PictureBox>(CropSide.Top, pbxKoala);
Then I could just wire all the buttons up to a single event handler like so:
private void btnC_Click(object sender, EventArgs e)
{
Button btnSend = (Button)sender;
KeyValuePair<CropSide, PictureBox> kvCrop = (KeyValuePair<CropSide, PictureBox>)btnSend.Tag;
cropPict(kvCrop.Value,kvCrop.Key);
}
Of course, there's still plenty more to do but that pretty much sorted out my problem. Thanks Hans!
Use the Button.Tag property to store a reference to its associated PictureBox. Cast sender to Button:
public Form1()
{
InitializeComponent();
button1.Tag = pictureBox1;
button1.Click += btnT_Click;
// etc..
}
private void btnT_Click(object sender, EventArgs e)
{
var btn = (Button)sender;
cropPict((PictureBox)btn.Tag, CropSide.Top);
}

Categories