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, :)
Related
I'm a beginner in developing c# windows form in visual studio and i'm a little bit confused with its programming constructs compared to VB.NET that i'm used to program. I want to know how to display multiple images in a single picturebox because in vb.net you will just import the image then load it from the resources not like in c# that I think the coding is different. Any help would be appreciated.
I would create an ImageList with all the Images you want to show in your PictureBox, add the three Buttons to your Form and change the Picture on Click.
public partial class Form1 : Form
{
private ImageList imagelst;
public Form1()
{
InitializeComponent();
imagelst = new ImageList();
}
private void Form1_Load(object sender, EventArgs e)
{
//pictures from your Harddrive
Image i = new Bitmap("rock.jpg");
imagelst.Images.Add("rock", i);
i = new Bitmap("scissors.jpg");
imagelst.Images.Add("scissors", i);
i = new Bitmap("paper.jpg");
imagelst.Images.Add("paper", i);
}
private void btnRock_Click(object sender, EventArgs e)
{
pictureBox1.Image = imagelst.Images["rock"];
}
private void btnScissors_Click(object sender, EventArgs e)
{
pictureBox1.Image = imagelst.Images["scissors"];
}
private void btnPaper_Click(object sender, EventArgs e)
{
pictureBox1.Image = imagelst.Images["paper"];
}
}
I hope I got what you want to do. If not excuse my bad english and slow-wittedness, please.
First off, thanks for reading this and taking the time to try help us.
We are currently making this project for our c# class, we had to study Drag&Drop by ourselves but we are having a problem.
Situation:
We have 6 starting pictureboxes with images, we also have 6 textboxes with a description, the idea is simple, match them together. However, if we drag an image from the starting picturebox into one of the 6 answer pictureboxes but by accident drop it on the form, it disappears.
We are clueless to find a way to actually 'reset' the image back to starting position when not placed in a picturebox.
private void Picture_MouseDown(object sender, MouseEventArgs e)
{
try
{
PictureBox source = (PictureBox)sender;
naam = source.ImageLocation;
source.DoDragDrop(source.Image, DragDropEffects.Move);
source.Image = null;
}
catch (NullReferenceException)
{
try
{
throw new RijException("Picturebox is momenteel leeg.");
}
catch (RijException)
{
}
}
}
private void Picture_DragEnter(object sender, DragEventArgs e)
{
PictureBox source = (PictureBox)sender;
e.Effect = DragDropEffects.Move;
}
private void Picture_DragDrop(object sender, DragEventArgs e)
{
PictureBox source = (PictureBox)sender;
source.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
source.Tag = naam;
}
So when we actually drop the image (while dragging) outside the picturebox (in the form) It will just disappear, What we need is a solution to reset it to starting position, but don't know how we could manage to pull that off.
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.
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);
}
I want to know that how can we drag item from treeview in C# and drop in other(autocad) application. That item is basically the autocad file .dwg.
I have written some code:
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
TreeNode n = (TreeNode)e.Item;
this.treeView1.DoDragDrop(AcadObj, DragDropEffects.Copy);
}
AcadObj is an autocad object.
This event is working fine but the event DragDrop is not firing, even when i drag on autocad the mouse pointer gives me plus sign (that the file is accepted here). My DragDrop event is :
private void treeview1_DragDrop(object sender, DragEventArgs e){
MessageBox.Show("It works");
}
The above event is not working and not showing me MessageBox. I have implemented only these two events.
Please helpe me and guide me how can i do this
The problem is because of memory space. You see, .NET stores its memory inside the CLR. This means , you cannot drag drop anything from .NET into another application running in a different memory space using the .NET dragdrop.
You have to use an interprocess drag-drop.
WINOLEAPI DoDragDrop(
IDataObject * pDataObject, //Pointer to the data object
IDropSource * pDropSource, //Pointer to the source
DWORD dwOKEffect, //Effects allowed by the source
DWORD * pdwEffect //Pointer to effects on the source
);
If you wrap the object you want to drag drop within your own implementation of IDataObject, you can drag drop into any application.
I would post an example, but i cant find one in my source which is "clean" enough to post as an example. Google around. Look for drag-drop implementations using C++ COM. Use that instead of the .NET built in drag drop.
Actually, the OP did not relay all the details. First, he is more than likely already running in the same memory space since 99% of all AutoCAD related programming is done in-process. Second, what version of AutoCAD and what verson of .NET? Without knowing these two key pieces of data, any answer may not be correct.
Having said that, the DragDrop event on the treeview control is never going to fire if you drop into AutoCAD - the event is for dropping on the treeview! When dealing with AutoCAD, calling the DoDrop from treeview is also not the best option. What you should be doing is calling the AutoCAD Application's DoDragDrop event:
AcApp.DoDragDrop(source, data, DragDropEffects.All, New DropTargetNotifier())
The DropTargetNotifier handles the dropped data and would be where you place your messagebox
To make your TreeView control be able to Drag and Drop to AutoCAD drawing area, you will need to host/embed your control in AutoCAD's PaletteSet: Here is my sample (I'm using ListBox here i.e. LB):
public dragdropentity TestLB; //dragdropentity is my actuall control containing my ListBox
[CommandMethod("ListBox")]
public void lb()
{
if (this.TestLB == null)
{
myPaletteSet = new PaletteSet("Test ListBox", new Guid("{B32639EE-05DF-4C48-ABC4-553769C67995}"));
TestLB = new dragdropentity();
myPaletteSet.Add("LB", TestLB);
}
myPaletteSet.Visible = true;
}
Once you are able to display your TreeView in PaletteSet, you can call AutoCAD's application DragDrop method. Here is the code segment from dragdropentity class:
public partial class dragdropentity : UserControl
{
public dragdropentity()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// MessageBox.Show(listBox1.SelectedIndex.ToString() + '\n' + listBox1.SelectedItem.ToString());
pictureBox1.Load(#"D:\My\Documents\Visual Studio 2010\Projects\ClassLibrary1\ClassLibrary1\Images\" + listBox1.SelectedItem.ToString() + ".png");
}
void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int indexOfItem = listBox1.IndexFromPoint(e.X, e.Y);
if (indexOfItem >= 0 && indexOfItem < listBox1.Items.Count) // check that an string is selected
{
listBox1.DoDragDrop(listBox1.Items[indexOfItem], DragDropEffects.Copy);
}
// throw new System.NotImplementedException();
}
void listBox1_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e)
{
ListBox lb = (ListBox)sender;
textBox1.AppendText('\n' + e.Action.ToString() + '\n'+ this.Name.ToString());
if (e.Action == DragAction.Drop)
{
Autodesk.AutoCAD.ApplicationServices.Application.DoDragDrop(this, "Drag & drop successful!!!", System.Windows.Forms.DragDropEffects.All, new DragDrop());
}
}
}
DragDrop() is your own class handling the Drop event. Here is my code:
class DragDrop : DropTarget
{
public override void OnDrop(System.Windows.Forms.DragEventArgs e)
{
using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
// MyCommands mc = new MyCommands();
// mc.CircleJig();
//Call your own methods etc here.
}
}