I am creating a digital clock user control. Here is the code:
public partial class DigitalClockControl : UserControl
{
public DigitalClockControl()
{
InitializeComponent();
}
private static List<Image> Sprite;
private static Clock data;
public Clock Data
{
get { return DigitalClockControl.data; }
set { DigitalClockControl.data = value;
int min = data.Min;
int sec = data.Sec;
Min1.Image = Sprite[min / 10];
Min2.Image = Sprite[min % 10];
Sec1.Image = Sprite[sec / 10];
Sec2.Image = Sprite[sec % 10];
}
}
private void DigitalClockControl_Load(object sender, EventArgs e)
{
Sprite = new List<Image>();
LoadSprite();
data = new Clock();
}
private void LoadSprite()
{
string path = Directory.GetParent((Directory.GetParent((Directory.GetCurrentDirectory().ToString())).ToString())).ToString();
Image img;
for (int i = 0; i <= 9; ++i)
{
img = Image.FromFile(path + "\\" + i.ToString() + ".png");
Sprite.Add(img);
}
}
}
When I tried to drag this user control to the form, it raised an error like this:
Failed to create component 'DigitalClockControl'. The error messages follows:'System.IO.FileNotFoundException: D:\\0.png...
I don't know why it loads an image from D:\. All the images are at the path above. If I copy the image to D:\, the program works fine. I tried to go to the InitializeComponent() function but cannot file any code makes the program load the image.
Edit: solved by adding user control by code in Form.cs. Thank you very much for all your help.
You should place all images into debug\\bin\\images folder. so, when you install the application you can find all the images in Application Folder (Application.StartupPat + "\\Images").
CODE:
string path = Application.StartupPath + "\\Images";
Image img;
for (int i = 0; i <= 9; ++i)
{
img = Image.FromFile(path + "\\" + i.ToString() + ".png");
Sprite.Add(img);
}
Alternatively, you could use Assembly.GetExecutingAssembly() (that is the assembly containing your control) and use its Location Property to figure out where the root of your component is. You could put the images for your clock into a folder next to your assembly. By doing so, deployment would be easy and clear.
Related
I'm plotting some scatter graphs from SQL data in a c# program. I would like automatically save these graphs as they populate. I have the following code which saves the Jpeg files, but when I open them, they are empty. I'm plotting multiple graphs at once.
Any help is appreciated.
public partial class XYplotForm : Form
{
public XYplotForm()
{
InitializeComponent();
}
public void Plot(Double[] freq, Double[] amp, Double[] bw, string name)
{
scatterGraph1.PlotXY(freq, amp);
tbName.Text = name;
Bitmap image = new Bitmap(scatterGraph1.Width, scatterGraph1.Height);
Rectangle target_bounds = default(Rectangle);
target_bounds.Width = scatterGraph1.Width;
target_bounds.Height = scatterGraph1.Height;
target_bounds.X = 0;
target_bounds.Y = 0;
scatterGraph1.DrawToBitmap(image, target_bounds);
string filename = "C:\\Graph\\" + name + ".Jpeg";
image.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
xyForm = new XYplotForm();
xyForm.Plot(freqLC40, ampMaxLC40, "LC-40 Max Amplitude");
xyForm.Show();
Bitmap image = new Bitmap(xyForm.Width, xyForm.Height);
System.Drawing.Rectangle target_bounds = default(System.Drawing.Rectangle);
target_bounds.Width = xyForm.Width;
target_bounds.Height = xyForm.Height;
target_bounds.X = 0;
target_bounds.Y = 0;
xyForm.DrawToBitmap(image, target_bounds);
string filename = "C:\\Graph\\LC40_Max Amplitude.Png";
image.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
I have this simple code in C# that basically implements a blur filter to an image based on the value of a Trackbar in Windows Forms.
private void BarraBlur_Scroll_1(object sender, EventArgs e)
{
// I take the image inside picture box and use my own Effects class
Bitmap image = new Bitmap(pictureBox1.Image);
Effects foto = new Effects(image);
switch (BarraBlur.Value)
{
case 0:
pictureBox1.Image = imagens; // This reestablishes the image to its original state
break;
case 1:
// Each case reestablishes the image to its original state
// and then applies the blur filter with a given depth
pictureBox1.Image = imagens;
pictureBox1.Image = foto.BlurEffect(1);
break;
case 2:
pictureBox1.Image = imagens;
pictureBox1.Image = foto.BlurEffect(2);
break;
case 3:
pictureBox1.Image = imagens;
pictureBox1.Image = foto.BlurEffect(3);
break;
case 4:
pictureBox1.Image = imagens;
pictureBox1.Image = foto.BlurEffect(4);
break;
case 5:
pictureBox1.Image = imagens;
pictureBox1.Image = foto.BlurEffect(5);
break;
default:
break;
}
}
What I want is that, depending on the value of the trackbar whenever it is set, to apply the blur filter to the image. The BlurEffect method takes an int argument so that the filter is applied in such value of depth.
The problem is that, if for example, the user sets the trackbar in the second position, it works fine, but when it sets it back to the first position, instead of returning the image to its original state and then applying the blur filter with depth 1, it applies a blur of depth 1 to the already blurred image with depth 2.
In other words, I want the trackbar to augment the depth of the filter each time the tick goes to the right and diminish the depth of the blur each time the tick of the trackbar goes left.
I have tried this with switch cases and if statements, but neither have worked.
Thanks a lot in advance.
This is the Effects class with the BlurEffect method.
class Effects
{
private Bitmap imagen;
// Constructor
public Effects(Bitmap item)
{
imagen = item;
}
public Bitmap BlurEffect(int depth)
{
for (int k = 0; k < depth; k++)
{
for (int i = 2; i < imagen.Width; i++)
{
for (int j = 2; j < imagen.Height; j++)
{
try
{
Color antX1 = imagen.GetPixel(i - 1, j);
Color antX2 = imagen.GetPixel(i - 2, j);
Color desX1 = imagen.GetPixel(i + 1, j);
Color desX2 = imagen.GetPixel(i + 2, j);
Color antY1 = imagen.GetPixel(i, j - 1);
Color antY2 = imagen.GetPixel(i, j - 2);
Color desY1 = imagen.GetPixel(i, j + 1);
Color desY2 = imagen.GetPixel(i, j + 2);
int promR = (int)((antX1.R + antX2.R + desX1.R + desX2.R + antY1.R + antY2.R + desY1.R + desY2.R + imagen.GetPixel(i, j).R) / 9);
int promG = (int)((antX1.G + antX2.G + desX1.G + desX2.G + antY1.G + antY2.G + desY1.G + desY2.G + imagen.GetPixel(i, j).G) / 9);
int promB = (int)((antX1.B + antX2.B + desX1.B + desX2.B + antY1.B + antY2.B + desY1.B + desY2.B + imagen.GetPixel(i, j).B) / 9);
imagen.SetPixel(i, j, Color.FromArgb(promR, promG, promB));
}
catch (Exception) { }
}
}
}
return imagen;
}
}
Firstly, you don't need the big switch with the repeated code. You can reduce your code to this:
if (BarraBlur.Value = 0)
{
pictureBox1.Image = imagens; // This reestablishes the image to its original state
}
else {
pictureBox1.Image = foto.BlurEffect(BarraBlur.Value);
}
However, this won't fix your problem. I don't know what class foto is, but it sounds like it is a wrapper for a Bitmap, and the BlurEffect() function applies a blur effect to that bitmap, before returning it. Your code appears to be written with the assumption that BlurEffect returns a blurred copy of the bitmap.
You can probably fix your code by changing it to construct a new foto every time you change the blur:
if (BarraBlur.Value = 0)
{
pictureBox1.Image = imagens; // This reestablishes the image to its original state
}
else {
var blur = new Foto(imagens); // or however the foto is constructed
pictureBox1.Image = blur.BlurEffect(BarraBlur.Value);
}
Since you've indicated that you wrote/control the foto class (named Effects), you could also change how that class works (instead of doing the above).
Sounds like currently it looks something like:
class Effects
{
private Bitmap _image;
public Effects(Bitmap image)
{
_image = image;
}
public Bitmap Blur(int amount)
{
// .. code to perform blur on _image ..
return _image;
}
}
And you should probably change it to be more like:
class Effects
{
private Bitmap _image;
public Effects(Bitmap image)
{
_image = image;
}
public Bitmap Blur(int amount)
{
var copy = _image.Clone();
// .. code to perform blur on copy ..
return copy;
}
}
pictureBox1.Image = imagens;
if (BarraBlur.Value == 0) return;
pictureBox1.Image = foto.BlurEffect(BarraBlur.Value);
Above replaces your whole code. Your problem is withing BlurEffect method. For that reason whenever you use 0 it will work because you will return image to the original. You need to reload image to the original as well at the start of BlurEffect method instead of what I'm guessing reusing picture.
I have created a program which takes .GIF images, seperates them into seperate files. Then there is a timer which calls a method to read a file in the sequence and changes the wallpaper. I was just wondering if there is anyway I can make this process take less time. (It works, just not fast enough...)
Note:
I know this may be unchangeable for the Desktop is not made for this kind of thing, but I'm wondering if there is maybe even an untidy dirty hack in which I could accomplish this...
Windows Forms C# Code:
private void CreateAllFramesInTemp()
{
for (int i = 0; i < TOTALFRAMES; i++)
{
GIF.SelectActiveFrame(GIFDIMENSION, i);
GIF.Save(tempPath + i.ToString() + ".bmp", ImageFormat.Bmp);
}
updateInterval.Start();
}
private void updateInterval_Tick(object sender, EventArgs e)
{
if (currentWallpaperFrame < TOTALFRAMES)
{
currentWallpaperFrame += 1;
}
else
{
currentWallpaperFrame = 1;
}
//_-_-_-_-This sets the wallpaper-_-_-_-_//
SystemParametersInfo(20, 0, tempPath + (currentWallpaperFrame - 1).ToString() + ".bmp", 0x01 | 0x02);
}
Ask me any questions about the code if you don't know what a specific part is.
In in powerpoint i have placed some diagrams diagrams, where each diagram contains different guid.And i have set the GUID as their hyperlink. when the refresh button is clicked what i do is ,will find the shape and then will get the guid which i have saved as hyperlink for each image , from each image and using that GUID will replace the recent image with the old image in that shape.
foreach (var shape in presentation.Slides[slideno].Shapes)
{
var slide = (PPT.Slide)item;
if (j <= shapeCount)
{
string[] address = new string[] { };
string dskj = slide.Shapes[j].Name;
if (slide.Shapes[j].Name.Equals("DIAGRAM")//, StringComparison.InvariantCultureIgnoreCase)
&& slide.Shapes[j].ActionSettings[PPT.PpMouseActivation.ppMouseClick].Hyperlink.Address != null)
{
address = slide.Shapes[j].ActionSettings[PPT.PpMouseActivation.ppMouseClick].Hyperlink.Address.Split('*');
string Type = address[0];
string Guid = address[1];
if (Type == "D")
{
Session.path = presentation.Path;
if (Session.path != "")
Session.Repository.GetProjectInterface().PutDiagramImageToFile(address[1], Session.path + "\\" + address[1] + ".jpg", 1);
bool diagrm = false;
try
{
EA.Diagram diag = Session.Repository.GetDiagramByGuid(Guid);
diagrm = true;
}
catch
{
continue;
}
if (diagrm)
{
float Shapeleft = slide.Shapes[j].Left;
float Shapetop = slide.Shapes[j].Top;
float Shapewidth = slide.Shapes[j].Width;
float Shapeheight = slide.Shapes[j].Height;
slide.Shapes[j].Delete();
PPT.Shape pic = slide.Shapes.AddPicture(Session.path + "\\" + Guid + ".jpg", Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoTrue, Shapeleft, Shapetop, Shapewidth, Shapeheight);
pic.Name = "DIAGRAM";
pic.ActionSettings[PPT.PpMouseActivation.ppMouseClick].Hyperlink.Address = "D*" + Guid;
}
}
}
}
using this above code everything works great.
address = slide.Shapes[j].ActionSettings[PPT.PpMouseActivation.ppMouseClick].Hyperlink.Address
in address i will get the hyperlink address of that current image,but now my problem is if i have two images in a same single slide , then both the time when it loops inside the shapes it only gives the same hyperlink for both the images.
**NOTE:**If i have only one image in a slide then everything works properly.
Its because you're deleting the previous shape and inserting a new shape at the same place , but each shape contains a ZOrderPosition , where because of you deleted the previous and inserted a new shape the Zorderposition get changed for the new shape and it will be included in the next iteration.So if more than one image is inserted in a shape it only refrsh some images .
SOLUTION:
After deleting the existing and when inserting a new shape set its ZorderPosition also.
pic.ZOrder(MsoZOrderCmd.msoSendToBack);
Hope it works..!!
Im using WMPLib to make an easy mp3player in C#. Im almost done but theres one more thing I want to do.
I Would like to how far gone the song is and also, how much is left of the song.
using for example the progressbar.
thanks
Adam
private void timer1_Tick(object sender, EventArgs e)
{
double percent = 0;
if (mp.Length != 0)
percent = ((double) wplayer.controls.currentPosition / wplayer.controls.currentItem.duration);
progressBar1.Value = (int)(percent * progressBar1.Maximum);
}
I have an Idea , just try to add statusStrip to your Project Form , and try to add a ToolStripStatusLabel and ToolStripProgressBar to it, and then you can use this simple code , it Works 100% :
public void Sound_Progress(ToolStripStatusLabel l1, ToolStripProgressBar psb)
{
//NASSIM LOUCHANI
int i = Convert.ToInt32(Player.controls.currentItem.duration);
int j = Convert.ToInt32(Player.controls.currentPosition);
int Defrence = (i-j);
l1.Text = Player.controls.currentPositionString + " | " + Player.controls.currentItem.durationString;
psb.Maximum = i;
psb.Minimum = 0;
if (Defrence == i)
psb.Value = i;
else if (Defrence != i)
psb.Value = Defrence;
else if (Defrence == 0)
l1.Text = "";
}
And don't forget to add a Timer to your Project Form and put the Sound_Progress(your ToolStripStatusLabel, your ToolStripProgressBar) into your Timer_Tick() Event .
Thank you !