How can I adapt the code in dll to the main code? - c#

I am triying the adapt my function which is found in dll but I am newbie on using dll and dividing the code.
The code in the dll is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace gdll
{
public class Class1
{
private Bitmap DoGray(Bitmap bmp)
{
for (int i = 0; i < bmp.Height - 1; i++)
{
for (int j = 0; j < bmp.Width - 1; j++)
{
int value = (bmp.GetPixel(j, i).R + bmp.GetPixel(j, i).G + bmp.GetPixel(j, i).B) / 3;
Color clr;
clr = Color.FromArgb(value, value, value);
bmp.SetPixel(j, i, clr);
}
}
return bmp;
}
}
}
But how should I write the code in form1.cs. Form1.cs[Design] have two buttons and 2 pictureBox.The first button for the original picture,the second one is for the filtered picture.I wrote the code version without dll(I wrote the function to the same page)below:
private void button2_Click(object sender, EventArgs e)
{
Bitmap image= new Bitmap(pictureBox1.Image);
Bitmap gray = DoGray(image);
pictureBox2.Image = gray;
}
Of course it is not working while function at dll file.
And this is the codes at form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using gdll;
namespace dnmimage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog sfd = new OpenFileDialog();
sfd.Filter = "Image Files|*.bmp|All Files|*.*";
sfd.InitialDirectory = ".";
if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return;
}
pictureBox1.ImageLocation = sfd.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap image= new Bitmap(pictureBox1.Image);
Bitmap gray = DoGray(image);
pictureBox2.Image = gray;
}

The DoGray function is private. In order to make it visible outside your Class1 it must be public. Further you should make it static:
public static Bitmap DoGray(Bitmap bmp)
Next step is to reference the gdll dll (the one with your DoGray function) needs to be referenced from the WinForms project (the one with the Form1.cs file).
The easiest way to do this is to have both projects (WinForms project and the dll project) in the same Solution and use a project reference.
Then right click on your WinForms project (the project that contains your Form1.cs file) and select “Add Reference…”
In the “Reference Manager” select “Projects” on the left side and check your gdll (the one with the Class1.cs file) and click ok.
Now you can use the DoGray function:
private void button2_Click(object sender, EventArgs e)
{
Bitmap image= new Bitmap(pictureBox1.Image);
Bitmap gray = gdll.Class1.DoGray(image);
pictureBox2.Image = gray;
}

Related

concatenate Rectangle control name for shape in c#

This is in WinForms. and i am using microsoft.vsualbasic.powerpacks
how can i concatenate Rectangle control name in c# this is what i have so far
string n = "1";
Rectangle match = this.Controls.Find("rectangleShape" + n,true)[0] as Rectangle;
match.BackColor = Color.Red;
Try some of the solutions
here... – Idle_Mind
okay will do try – droid fiji
it didnt really help :( – droid fiji
Here's a working example based on the solution I linked to in the comments. Note that the BackStyle property of your RectangleShape needs to be Opaque for you to see the color you set!
This code will set the BackColor of rectangeShape1 thru rectangeShape3 to Red:
using Microsoft.VisualBasic.PowerPacks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string rectName;
string rectBaseName = "rectangleShape";
var shapeContainer = this.Controls.OfType<ShapeContainer>().FirstOrDefault();
if (shapeContainer != null)
{
for (int i = 1; i <= 3; i++)
{
rectName = rectBaseName + i.ToString();
RectangleShape match = shapeContainer.Shapes.OfType<RectangleShape>().FirstOrDefault(o => o.Name == rectName);
if (match != null)
{
match.BackColor = Color.Red;
match.BackStyle = BackStyle.Opaque;
}
}
}
}
}
}

Continuously desktop region capture failed after some time - Unhandled exception in System.Drawing.dll (parameter is not valid)

I am making a an app where I have to capture selected desktop region continuously(using "Timer(CaptureTimer)" with interval 100) , resize it , make it to dither image & show it to "PictureBox(PreviewPictureBox)".
My app has two "Form"s.
One is "MainForm" contains "PreviewPictureBox" & "Button(StartButton)" & other is "CaptureForm" contains "CapturePictureBox".
"CaptureForm(size is 646x326)" is transparent & FromBorderStyle = none.
"CapturePictureBox" is "dock in parent container" & has a side bordered & middle transparent png picture in it.
"CaptureForm" can be move by click & dragging the "CapturePictureBox".
This is "MainForm"
Here is "MainForm" Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _128x64_GLCD_Monitor
{
public partial class MainForm : Form
{
CaptureForm CapF = new CaptureForm();
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
{
Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
Graphics graphics = Graphics.FromImage(ScreenCaptureBmp as Image);
graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
return ScreenCaptureBmp;
}
public Bitmap ResizeBitmap(Bitmap ResizeBmp, int RBmpWidth, int RBmpHeight)
{
Bitmap RBmp = new Bitmap(RBmpWidth, RBmpHeight);
using (Graphics RBmpG = Graphics.FromImage((Image)RBmp))
RBmpG.DrawImage(ResizeBmp, 0, 0, RBmpWidth, RBmpHeight);
return RBmp;
}
public Bitmap DitherBitmap(Bitmap DitherBmp) // Not writing full method here
private void StartButton_Click(object sender, EventArgs e)
{
CapF.Show();
CaptureTimer.Start();
}
private void CaptureTimer_Tick(object sender, EventArgs e)
{
int windowLeft = CapF.Left + 3;
int windowTop = CapF.Top + 3;
int windowWidth = CapF.Width - 6;
int windowHeight = CapF.Height - 6;
Bitmap Pic = ScreenCaptureBitmap(windowLeft, windowTop, windowWidth, windowHeight);
Bitmap Pic1 = ResizeBitmap(Pic, 128, 64);
Bitmap Pic2 = DitherBitmap(Pic1);
PreviewPictureBox.Image = Pic2;
}
}
}
This is "CaptureForm"
Here is "CaptureForm" Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _128x64_GLCD_Monitor
{
public partial class CaptureForm : Form
{
Boolean TogMove;
int MValX, MValY;
public CaptureForm()
{
InitializeComponent();
}
private void CaptureForm_Load(object sender, EventArgs e)
{
TransparencyKey = BackColor;
}
private void CapturePictureBox_MouseDown(object sender, MouseEventArgs e)
{
TogMove = true;
MValX = e.X;
MValY = e.Y;
}
private void CapturePictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (TogMove == true)
{
this.SetDesktopLocation(MousePosition.X - MValX, MousePosition.Y - MValY);
}
}
private void CapturePictureBox_MouseUp(object sender, MouseEventArgs e)
{
TogMove = false;
}
}
}
When I start debugging, after some time(after completing some loop) it getting this message
Visual Studio 2010 pointed this line(at a 1 time debugging 1 line)
Some time this line
Bitmap ScreenCaptureBmp = new Bitmap(CaptureWidth, CaptureHeight);
Or some time this line
graphics.CopyFromScreen(DesktopX, DesktopY, 0, 0, ScreenCaptureBmp.Size);
From this method
public Bitmap ScreenCaptureBitmap(int DesktopX, int DesktopY, int CaptureWidth, int CaptureHeight)
What I have to do to prevent this error?
Make sure the following 4 variables are within the size of the Form. When you click the numbers can be negative or greater than the size (width , height) of the form. :
int windowLeft = CapF.Left + 3;
int windowTop = CapF.Top + 3;
int windowWidth = CapF.Width - 6;
int windowHeight = CapF.Height - 6;
It looks like the values for windowLeft, WindowTop, windowWidth or windowHeight in your timer methode, went negative.
If you only want to prevent this error, you can simply set the values in an if-clause before you call the method ScreenCaptureBitmap(...) or you prevent the dragging to negative values in the LocationChanges-Event.

Edit the background of a button in c#

I have a little problem of mine to solve. I have a button which has an image background.I tried to color the whole button but the image can not be seen after coloring the whole button. How can I edit this "imagebutton" like in this example? http://i.stack.imgur.com/XaQQQ.png
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace bura
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.BackgroundImage != null)
{
button2.BackgroundImage = null;
button2.BackColor = Color.Black;
}
else {
button2.BackgroundImageLayout = ImageLayout.Stretch;
button2.BackgroundImage = Image.FromFile("C:\\Users\\rati\\Desktop\\ks.png");
}
}
private void button3_Click(object sender, EventArgs e)
{
button2.BackgroundImageLayout = ImageLayout.Stretch;
button2.BackgroundImage = Image.FromFile("C:\\Users\\rati\\Desktop\\ks.png");
}
}
}
This
Just made a button by using the designer with the following code:
this.button1.BackColor = System.Drawing.Color.DodgerBlue;
this.button1.BackgroundImage = global::WindowsFormsApplication.Properties.Resources.ChargeImage;
this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.button1.Image = global::WindowsFormsApplication.Properties.Resources.DatabaseImage;
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(264, 160);
this.button1.TabIndex = 0;
this.button1.UseVisualStyleBackColor = false;
And that is the result:
So what is your problem?
You can edit your pictures with a method like this:
private static void DrawLinesOnBitmap(Bitmap bmp)
{
using (var p = new Pen(Color.Black, 5))
{
using (var g = Graphics.FromImage(bmp))
{
g.DrawLine(p, 0, 0, bmp.Width, bmp.Height);
}
}
}
This method adds a line from the left top corner to the right bottom corner. Just draw some more lines and you should get your wanted result.

Why when creating screenshot captured files on hard disk and make of them avi it's very slow?

What i mean is i have this code i'm creating many gif files on the hard disk using a timer to capture the desktop window. Every 1 second i'm taking a screenshot.
For example after 60 seconds i have on the hard disk only 60 gif files.
Then i'm using my own animated gif class to create animated gif.
When i'm playing the animated gif it seems that many frames are missing.
Now i'm taking screenshots of the dekstop window every second a screenshot.
But i think i should take every second like 25 screenshots or 60 ?
How can i do it ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.IO;
using unfreez_wrapper;
namespace CapturedDesktop
{
public partial class Form1 : Form
{
int screens;
string gifsdirectory;
UnFreezWrapper unfreez;
public Form1()
{
InitializeComponent();
unfreez = new UnFreezWrapper();
screens = 0;
gifsdirectory = #"C:\Temp\CapturedDesktop\";
}
private void CaptureScreenshot()
{
screens++;
screenshots(gifsdirectory + screens.ToString("D6") + ".gif");
}
public static void screenshots(string filename)
{
//Create a new bitmap.
using (var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width - 100,
Screen.PrimaryScreen.Bounds.Height - 100,
PixelFormat.Format32bppArgb))
{
// Create a graphics object from the bitmap.
using (var gfxScreenshot = Graphics.FromImage(bmpScreenshot))
{
// Take the screenshot from the upper left corner to the right bottom corner.
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
}
// Save the screenshot to the specified path that the user has chosen.
bmpScreenshot.Save(filename, ImageFormat.Gif);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
CaptureScreenshot();
}
private void AnimatedGifButton_Click(object sender, EventArgs e)
{
List<string> myGifList = new List<string>();
FileInfo[] fi;
DirectoryInfo dir1 = new DirectoryInfo(gifsdirectory);
fi = dir1.GetFiles("*.gif");
for (int i = 0; i < fi.Length; i++)
{
myGifList.Add(fi[i].FullName);
}
unfreez.MakeGIF(myGifList, gifsdirectory + "agif", 100, true);
}
}
}
The problem is not the animated gif speed when i create it but it's not smooth it's like missing frames.

How can i save an image every second of a WebBrowser page url content ?

Im using this code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Drawing.Imaging;
namespace DownloadFlashFiles
{
public partial class Form1 : Form
{
int i = 0;
public Form1()
{
InitializeComponent();
GenerateScreenshot("http://www.nana10.co.il");
}
private void Form1_Load(object sender, EventArgs e)
{
}
public Bitmap GenerateScreenshot(string url)
{
// This method gets a screenshot of the webpage
// rendered at its full size (height and width)
return GenerateScreenshot(url, -1, -1);
}
public Bitmap GenerateScreenshot(string url, int width, int height)
{
// Load the webpage into a WebBrowser control
//WebBrowser wb = new WebBrowser();
webBrowser1.ScrollBarsEnabled = false;
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate(url);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
// Set the size of the WebBrowser control
webBrowser1.Width = width;
webBrowser1.Height = height;
if (width == -1)
{
// Take Screenshot of the web pages full width
webBrowser1.Width = webBrowser1.Document.Body.ScrollRectangle.Width;
}
if (height == -1)
{
// Take Screenshot of the web pages full height
webBrowser1.Height = webBrowser1.Document.Body.ScrollRectangle.Height;
}
// Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Bitmap bitmap = new Bitmap(webBrowser1.Width, webBrowser1.Height);
webBrowser1.DrawToBitmap(bitmap, new Rectangle(0, 0, webBrowser1.Width, webBrowser1.Height));
//webBrowser1.Dispose();
return bitmap;
}
private void timer1_Tick(object sender, EventArgs e)
{
i++;
Bitmap thumbnail = GenerateScreenshot("http://www.nana10.co.il");
thumbnail.Save(#"d:\test4\" + i.ToString("D6") + "bmp", ImageFormat.Bmp);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
}
}
The problem is when im clicking the button i see the content in the WerBrowser loading every second but it's never get to the code inside the timer1 tick event since it's stuck in the While loop in this line:
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
What i want is to load a new content ot the webbrowser every second and also save the new content from the webbrowser every second to the hard disk.
How an i do it since it's stucking in the While loop ?
Check this if it helps you, first link does what you want
http://www.codeproject.com/Articles/15211/Webpage-thumbnailer
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/d9b69701-85ba-439c-9737-e36f78f65acf/

Categories