Image Slider in windows app - c#

I am working on a windows form application.
I want a image slider to be displayed at the bottom of my windows application, something like this:
Image slide example
Image paths would be grabbed from a DB. Any idea how to implement this?
Thanks in advance :)

I don't know if you want a slider that shows only one picture at a time or more, but you can adapt the code if you need the latter.
private void showImage(string path)
{
Image imgtemp = Image.FromFile(path);
pictureBox1.Width = imgtemp.Width / 2;
pictureBox1.Height = imgtemp.Height / 2;
pictureBox1.Image = imgtemp;
}
If you want it to work only on an automated mode, use only one method:
private void prevImage()
{
if(selected == 0)
{
selected = folderFile.Length - 1;
showImage(folderFile[selected]);
}
else
{
selected = selected - 1; showImage(folderFile[selected]);
}
}
private void nextImage()
{
if(selected == folderFile.Length - 1)
{
selected = 0;
showImage(folderFile[selected]);
}
else
{
selected = selected + 1; showImage(folderFile[selected]);
}
}
Now the timer and the start slideshow button:
private void timer1_Tick(object sender, System.EventArgs e)
{
nextImage();
}
private void Start_Click(object sender, System.EventArgs e)
{
if(timer1.Enabled == true)
{
timer1.Enabled = false;
Start.Text = "<< START Slide Show >>";
}
else
{
timer1.Enabled = true;
Start.Text = "<< STOP Slide Show >>";
}
}
From here. However, if you need something more than this you can read check ImageSlider from devexpress.

Related

PictureBox not changing the image [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a pictureBox that I use as a button. When I start the form I load it as disabled and after pressing a button I activate it, it works to change the image from disabled to activated. Then, when I disable this pictureBox again, the image doesn't change anymore ... what could be wrong?
here's my code:
private void btnUpdate_EnabledChanged(object sender, EventArgs e)
{
if (btnUpdate.Enabled == true)
{
if (mt.ArquivoExiste(Metodos.pathImagens, botaoUpdateNormal))
{
Image bt = Image.FromFile(Metodos.pathImagens + botaoUpdateNormal);
btnUpdate.BackgroundImage = bt;
}
}
else
{
if (mt.ArquivoExiste(Metodos.pathImagens, botaoUpdateDisabled))
{
Image bt = Image.FromFile(Metodos.pathImagens + botaoUpdateDisabled);
btnUpdate.BackgroundImage = bt;
}
}
}
Edit:
I changed the string to make that easier and put the entire relationated code:
string botaoUpdateNormal = "btnUpdate_normal.png", botaoUpdateDisabled = "btnUpdate_disabled.png",
botaoUpdateFocus = "btnUpdate_focus.png", botaoSearchNormal = "btnSearch_normal.png",
botaoSearchFocus = "btnSearch_focus.png", botaoInsertNormal = "btnInsert_normal.png",
botaoInsertFocus = "btnInsert_focus.png";
then i load the form:
private void IEstoque_Load(object sender, EventArgs e)
{
if (mt.ArquivoExiste(Metodos.pathImagens, botaoUpdateDisabled))
{
Image bt = Image.FromFile(Metodos.pathImagens + botaoUpdateDisabled);
btnUpdate.BackgroundImage = bt;
}
}
After that I have an event that when I change the row of a grid, the btnUpdate activates, and when I click on it and update my dataBase it desactivates.
This code work for pictureBox "like button" and with separate button:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.pictureBox.Enabled = false;
this.pictureBox.EnabledChanged += new System.EventHandler(this.pictureBox_EnabledChanged);
this.pictureBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseClick);
this.button.Click += new System.EventHandler(this.button_Click);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
}
private void pictureBox_EnabledChanged(object sender, EventArgs e)
{
if (pictureBox.Enabled == true)
{
var botao = "1.png";
Image bt = Image.FromFile(botao);
pictureBox.BackgroundImage = bt;
}
else
{
var botao = "2.png";
Image bt = Image.FromFile(botao);
pictureBox.BackgroundImage = bt;
}
}
private void button_Click(object sender, EventArgs e)
{
pictureBox.Enabled = !pictureBox.Enabled;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (!MouseIsPicture(e.Location)) return;
TogglePicture();
}
private void pictureBox_MouseClick(object sender, MouseEventArgs e)
{
TogglePicture();
}
private bool MouseIsPicture(Point location)
{
// Make sure the location is over the image.
if (location.X < 0) return false;
if (location.Y < 0) return false;
if (location.X >= pictureBox.Width) return false;
if (location.Y >= pictureBox.Height) return false;
return true;
}
void TogglePicture()
{
pictureBox.Enabled = !pictureBox.Enabled;
}
}
More over: check if pictures you are loading are not the same
I solve it! before deactivating the button I called a form as follows: formname.Show ();
I just changed it to formname.ShowDialog (); and it worked normally.

How to make automated image slideshow using imagelist & timer in C#?

How to make slideshow to automaticly changing pictures in picturebox? currently im using imagelist but it didnt seems to works. I want the timer to change the image every 3 seconds. this is the timer code I'm using. it works well before to click buttons every 3 seconds but not working with imagelist. I'm new with imagelist & slideshows so if have any suggestion about it please tell me. thanks.
private bool _timerEnabled;
private async Task StartTimer()
{
_timerEnabled = true;
int i = 0;
while (_timerEnabled)
{
i++;
if (i > 2) { i = 0; }
pictureBox2.Image = imageList1.Images[i];
bmp = new Bitmap(pictureBox2.Image, pictureBox2.Width, pictureBox2.Height);
pictureBox1.Refresh();
pictureBox2.Refresh();
await Task.Delay(3000);
}
}
private async void timerStartButton_Click(object sender, EventArgs e)
{
timerStopButton.Enabled = true;
timerStartButton.Enabled = false;
if (_timerEnabled)
return;
await StartTimer();
}
private void timerStopButton_Click(object sender, EventArgs e)
{
timerStopButton.Enabled = false;
timerStartButton.Enabled = true;
_timerEnabled = false;
}
the slideshow works fine now but the used image become blurry. How to fix this one?
Original Image
Blurry result
EDIT.
after checking it again the blurry result is from imagelist control that automaticly set the image size to 16,16. it seems cant get it any bigger than 320,320. know how to make it can use bigger resolution size?
Simply move int i out of the while loop.
private async Task StartTimer() {
_timerEnabled = true;
int i = 0; //Move this here
while (_timerEnabled) {
i++;
if (i > 2) { i = 0; }
pictureBox2.Image = imageList1.Images[i];
pictureBox1.Refresh();
pictureBox2.Refresh();
await Task.Delay(3000);
}
}
Or you can use the inbuilt timer control instead of reinventing the wheel.
Timer timer1;
int i = 0;
//Form's constructor
public Form1
{
timer1 = new Timer();
timer1.Interval = 3000;
timer1.Tick += new EventHandler(timer1_tick);
}
private void timer1_tick(object sender, EventArgs e)
{
i++;
if (i > 2) { i = 0; }
pictureBox2.Image = imageList1.Images[i];
pictureBox1.Refresh();
pictureBox2.Refresh();
}
private async void timerStartButton_Click(object sender, EventArgs e) {
timerStopButton.Enabled = true;
timerStartButton.Enabled = false;
if (timer1.Enabled) return;
timer1.Enabled = True;
}
private void timerStopButton_Click(object sender, EventArgs e) {
timerStopButton.Enabled = false;
timerStartButton.Enabled = true;
timer1.Enabled = false;
}

How to set size of object on form in c#?

I have panel and Datagridview on Form, panel is for sliding up and down to show and hide its contents.
When I click on show button it executes this code:
private void button1_Click(object sender, EventArgs e)
{
if (hidded)
{
button1.Visible = false;
button2.Visible = true;
}
else
{
button1.Visible = true;
button2.Visible = false;
}
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (hidded)
{
Spanel.Height = Spanel.Height + 20;
Datagridview1.Location = new Point(23 , Datagridview1.Location.Y + 20);
if (Spanel.Height >= 140)
{
timer1.Stop();
hidded = false;
this.Refresh();
}
}
else
{
Spanel.Height = Spanel.Height - 20;
Datagridview1.Location = new Point( 23, Datagridview1.Location.Y - 20);
if (Spanel.Height <= 0)
{
timer1.Stop();
hidded = true;
this.Refresh();
}
}
}
when i try to hide/close panel the Datagridview moves up and become like this:
I just need to fix anchor size or datagridview location from down.
If you want to do it your way (with timer) just change your timer tick event handler to the code bellow. It will also change the size of the DataGridView alongside with its position.
private void timer1_Tick(object sender, EventArgs e)
{
if (hidded)
{
Spanel.Height = Spanel.Height + 20;
Datagridview1.Location = new Point(23, Datagridview1.Location.Y + 20);
Datagridview1.Size = new Size(Datagridview1.Width, Datagridview1.Height - 20);
if (Spanel.Height >= 140)
{
timer1.Stop();
hidded = false;
this.Refresh();
}
}
else
{
Spanel.Height = Spanel.Height - 20;
Datagridview1.Location = new Point(23, Datagridview1.Location.Y - 20);
Datagridview1.Size = new Size(Datagridview1.Width, Datagridview1.Height + 20);
if (Spanel.Height <= 0)
{
timer1.Stop();
hidded = true;
this.Refresh();
}
}
}
My approach to this problem would be a bit different and if u don't mind i will let it here. Instead of anchoring i would do it like this(see the pic bellow) using docking. It should work the same using the code you posted (Your SPanel is Panel2 on the picture).
Edit #1: For fluently moving or resizing controls in your WinForm app i recommend you to use this library: https://github.com/UweKeim/dot-net-transitions. Using the mentioned library your button click event hanlder would look something like this:
private bool resizing = false;
private void button1_Click(object sender, EventArgs e)
{
if (resizing)
return;
resizing = true;
Transition t = new Transition(new TransitionType_Acceleration(600));
t.TransitionCompletedEvent += (snd, ea) => { resizing = false; };
t.add(panel2, "Height", panel2.Height == 0 ? 250 : 0);
t.run();
}

Horizontal swipe gesture on UWP

is there any way to get swipe gesture same as windows phone 8 with wptoolkit.
Because wptoolkit nuget package is not available for uwp, so i am unable to get similar swipe gesture on UWP
In windows Phone 8 with the help of WPtoolkit nugetget package
i placed this
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Flick="OnSwipe"/>
</toolkit:GestureService.GestureListener>
over text block, so i can swipe left to right or right to left over textbox1.
and swipe gesture help me to implement this
private static int i;
private void OnSwipe(object sender, FlickGestureEventArgs e)
{
if (e.HorizontalVelocity < 0)
{
i++;
txtBox1.Text = i.ToString();
}
if (e.HorizontalVelocity > 0)
{
i--;
txtBox1.Text = i.ToString();
}
}
i tried Manupulation method with scrollViewer on uwp but it continuously increase the value untill it scroll viewer stopped
and this is codes
private static int i;
private Point initialpoint;
private void scrollview_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
initialpoint = e.Position;
}
private void scrollview_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (e.IsInertial)
{
Point currentpoint = e.Position;
if (currentpoint.X - initialpoint.X >= 2)
{
i++;
txtBox1.Text = i.ToString();
}
if (currentpoint.Y - initialpoint.Y >= 2)
{
i--;
txtBox1.Text = i.ToString();
}
}
}
Any other way to implement same functionality.
Actually you don't need to handle ManipulationStarted in this case and you don't need the initialPoint property. Assuming you have already defined your ScrollViewer's ManipulationMode to the following
ManipulationMode="TranslateX,TranslateInertia,System"
Then you simply use e.Cumulative.Translation.X to tell how long you have swiped in total
private void scrollview_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
if (e.IsInertial)
{
var swipedDistance = e.Cumulative.Translation.X;
if (Math.Abs(swipedDistance) <= 2) return;
if (swipedDistance > 0)
{
i++;
}
else
{
i--;
}
txtBox1.Text = i.ToString();
}
}
Update
Now that I understand your question better, I think you should handle gesture manipulation on the TextBox itself. If you want instant feedback, simply subscribe to the ManipulationDelta event and create a flag to only run the swipe logic once per touch.
private bool _isSwiped;
private void txtBox1_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (e.IsInertial && !_isSwiped)
{
var swipedDistance = e.Cumulative.Translation.X;
if (Math.Abs(swipedDistance) <= 2) return;
if (swipedDistance > 0)
{
i++;
}
else
{
i--;
}
txtBox1.Text = i.ToString();
_isSwiped = true;
}
}
private void txtBox1_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
_isSwiped = false;
}
Make sure you move all the handlers and set the ManipulationMode onto the TextBox.
<TextBox x:Name="txtBox1"
ManipulationMode="TranslateX,TranslateInertia,System"
ManipulationDelta="txtBox1_ManipulationDelta"
ManipulationCompleted="txtBox1_ManipulationCompleted" />

Rotate Image and View Next Page

I’m using WinForms for my application. I’m building an image viewer. My application opens image documents (.tif) files. The application has the ability to go to the next page.
The issue is, that every time I try to rotate the image and click next, the image stays on the same page but the page number increments.
Why can’t I see the images when it’s on rotate?
How can I rotate the image and go to the next page?
In the link below I've provided a test tif document for testing purposes:
http://www.filedropper.com/sampletifdocument5pages
My Code:
FileStream _stream;
Image _myImg; // setting the selected tiff
string _fileName;
private int intCurrPage = 0; // defining the current page
private int intTotalPages = 0;
private void Open_Btn_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
lblFile.Text = openFileDialog1.FileName;
// Before loading you should check the file type is an image
if (_myImg == null)
{
_fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
File.Copy(#lblFile.Text, _fileName);
_stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read);
pictureBox1.Image = Image.FromStream(_stream);
}
//pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
pictureBox1.Size = new Size(750, 1100);
// Reset the current page when loading a new image.
intCurrPage = 1;
intTotalPages = pictureBox1.Image.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
lblNumPages.Text = intTotalPages.ToString();
lblCurrPage.Text = "1";
}
}
private void NextPage_btn_Click(object sender, EventArgs e)
{
if (intCurrPage <= (intTotalPages - 1))
{
if(Radio_90_Rotate.Checked)
{
pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
if(Radio_180_Rotate.Checked)
{
pictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
}
// Directly increment the active frame within the image already in the PictureBox
pictureBox1.Image.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, intCurrPage);
//page increment (Go to next page)
intCurrPage++;
// Refresh the PictureBox so that it will show the currently active frame
pictureBox1.Refresh();
lblCurrPage.Text = intCurrPage.ToString();
}
}
The RotateFlip function will change the source image and flatten it to only one page. This means we need to make copies each time you view a new page that has rotation applied.
In this solution, I use the source image and simply change pages when no rotation is applied. But when rotation is set, then a Image copy is made for each page and then the rotation is applied to the copy only.
Using your sample image it takes time to load each page. So I implemented a simple label message to let the user know it's working.
Also, you may consider looking into classes prebuilt for tiff files like: https://bitmiracle.github.io/libtiff.net/
private Image _Source = null;
private int _TotalPages = 0;
private int _CurrentPage = 0;
private void Frm_TiffViewer_Load(object sender, EventArgs e)
{
lbl_WaitMessage.Visible = false;
// These two options can be adjusted as needed and probably should be set in the form control properties directly:
pictureBox1.Size = new Size(750, 1100);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
private void ShowProcessingImageLabel()
{
lbl_WaitMessage.Visible = true;
Application.DoEvents();
}
private void DisplayPage(int PageNumber, RotateFlipType Change)
{
if (pictureBox1.Image != null && pictureBox1.Image != _Source)
{
// Release memory for old rotated image
pictureBox1.Image.Dispose();
}
// set the variable to null for easy GC cleanup
pictureBox1.Image = null;
_Source.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, PageNumber - 1);
pictureBox1.Image = new Bitmap(_Source);
pictureBox1.Image.RotateFlip(Change);
pictureBox1.Refresh();
}
private void DisplayPage(int PageNumber)
{
ShowProcessingImageLabel();
this.lblCurrPage.Text = PageNumber.ToString();
// You could adjust the PictureBox size here for each frame OR adjust the image to fit the picturebox nicely.
if (Radio_90_Rotate.Checked == true)
{
DisplayPage(PageNumber, RotateFlipType.Rotate90FlipNone);
lbl_WaitMessage.Visible = false;
return;
}
else if (Radio_180_Rotate.Checked == true)
{
DisplayPage(PageNumber, RotateFlipType.Rotate180FlipNone);
lbl_WaitMessage.Visible = false;
return;
}
if (pictureBox1.Image != _Source)
{
if (pictureBox1.Image != null)
{
// Release memory for old copy and set the variable to null for easy GC cleanup
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
pictureBox1.Image = _Source;
}
pictureBox1.Image.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, PageNumber-1);
pictureBox1.Refresh();
lbl_WaitMessage.Visible = false;
}
private void Open_Btn_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// Before loading you should check the file type is an image
this._Source = Image.FromFile(openFileDialog1.FileName);
_TotalPages = _Source.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
_CurrentPage = 1;
lblCurrPage.Text = "1";
lblFile.Text = openFileDialog1.FileName;
this.lblNumPages.Text = _TotalPages.ToString();
DisplayPage(_CurrentPage);
}
}
private void NextPage_btn_Click(object sender, EventArgs e)
{
if (_CurrentPage < _TotalPages)
{
_CurrentPage++;
}
DisplayPage(_CurrentPage);
}
private void b_Previous_Click(object sender, EventArgs e)
{
if (_CurrentPage > 1)
{
_CurrentPage--;
}
DisplayPage(_CurrentPage);
}
private void Radio_90_Rotate_CheckedChanged(object sender, EventArgs e)
{
DisplayPage(_CurrentPage);
}
private void Radio_180_Rotate_CheckedChanged(object sender, EventArgs e)
{
DisplayPage(_CurrentPage);
}
private void Radio_0_Default_CheckedChanged(object sender, EventArgs e)
{
DisplayPage(_CurrentPage);
}
I`m Created Custom Mode v3.1
public Image _Image_v3_1_CustomMode(Image b1, float angle,float dx,float dy,float sx,float sy)
{
Bitmap bitmap = new Bitmap(b1.Width, b1.Height);
using(Graphics ehack = Graphics.FromImage(bitmap))
{
ehack.RotateTransform(angle);
ehack.TranslateTransform(dx, dy);
ehack.ScaleTransform(sx, sy);
ehack.DrawImage(b1, 0, 0);
return bitmap;
}
}

Categories

Resources