I am a newer of C#,I want to use WebBrowser control to copy an image and save to local disk,after I googled in stackoverflow is this code I need to use,but I am a newer,could anyone can provider a Full C# Codes to make it work?(ConsoleApplication type),thanks in advance.
I want to COPY in webbrowser (not download) this image file
to
C:\google.png
The source is here:
WebBrowser Copy Image to Clipboard
string image_name = "temp.bmp";
IHTMLDocument2 document = (IHTMLDocument2)webBrowser1.Document.DomDocument;
IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)document.body).createControlRange();
imgRange.add(document.all.item(HTML_IMAGE_ID));
imgRange.execCommand("Copy");
using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
{
bmp.Save(image_name);
}
Here is the full code of working sample
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com");
}
private void button2_Click(object sender, EventArgs e)
{
IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.DomDocument;
IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
foreach (IHTMLImgElement img in doc.images)
{
imgRange.add((IHTMLControlElement)img);
imgRange.execCommand("Copy", false, null);
using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
{
bmp.Save(#"C:\" + img.nameProp);
}
}
}
You need to add namespace using mshtml;
Related
I've got problem with moving selected file in listview using button to another folder, my code:
public void button2_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(#"C:\Users\Mkzz\Desktop\doki");
string destinyFodler = #"C:\Users\Mkzz\Desktop\doki\test1\*.tif";
listView1.Dispose();
foreach (string file in files)
{
File.Move(file, destinyFodler);
}
}
It gives me error „The process cannot access the file because it is being used by another process.”. Files loaded to listview are .tif ' s images, they are also loaded into picturebox.
Is there any way to fix this?
Try loading the PictureBox controls using a clone image e.g.
public class ImageHelpers
{
public static Image LoadImageClone(string Path)
{
Bitmap imageClone = null;
var imageOriginal = Image.FromFile(Path);
imageClone = new Bitmap(imageOriginal.Width, imageOriginal.Height); // create clone, initially empty, same size
using (var gr = Graphics.FromImage(imageClone))
{
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
gr.DrawImage(imageOriginal, 0, 0, imageOriginal.Width, imageOriginal.Height); //copy original image onto this surface
}
imageOriginal.Dispose();
return imageClone;
}
}
Test we can delete the image after assigning the image to a PictureBox
private void LoadImageButton_Click(object sender, EventArgs e)
{
pictureBox1.Image = ImageHelpers.LoadImageClone("Folders.png");
File.Delete("Folders.png");
}
Note I'm not the author of this code, picked the code up many years ago.
I am just learning C# and I was trying to implement a webcam picture capture program. I'm using the Aforge library, the thing is that my picturebox is not displaying the webcam image and I don't understand why. If anyone knows my error, please let me know. Thank you in advance.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DarrenLee.Media;
namespace test4
{
public partial class MainForm : Form
{
int count = 0;
Camera myCamera = new Camera();
public MainForm()
{
InitializeComponent();
GetInfo();
myCamera.OnFrameArrived += myCamera_OnFrameArrived;
}
private void GetInfo()
{
var cameraDevices = myCamera.GetCameraSources();
var cameraResolutions = myCamera.GetSupportedResolutions();
foreach (var d in cameraDevices)
cmbCameraDevices.Items.Add(d);
foreach (var r in cameraResolutions)
cmbCameraResolutions.Items.Add(r);
cmbCameraDevices.SelectedIndex = 0;
cmbCameraDevices.SelectedIndex = 0;
}
private void myCamera_OnFrameArrived(object source, FrameArrivedEventArgs e)
{
Image img = e.GetFrame();
picCamera.Image = img;
}
void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
myCamera.ChangeCamera(cmbCameraDevices.SelectedIndex);
}
void ComboBox2SelectedIndexChanged(object sender, EventArgs e)
{
myCamera.Start(cmbCameraDevices.SelectedIndex);
}
void Form1FormClosing(object sender, FormClosingEventArgs e)
{
myCamera.Stop();
}
void BTTsaveClick(object sender, EventArgs e)
{
string filename = Application.StartupPath + #"\" + "Image" + count.ToString();
myCamera.Capture(filename);
count++;
}
}
}
Picture of how it looks when I compile it:
https://i.gyazo.com/6956a07405cd4bf5e74c20bc321bd32e.png
I am connecting the picture box with the content in this line:
Image img = e.GetFrame();
picCamera.Image = img;
Make sure you click on the object before writing the code.
Example: Click on the button in the design form then paste the code for BTTsaveClick in the newly created method after clicking on the button.
If you are using Visual Studio, you can check if the methods are referenced. If it's not referenced, that means the code won't be read.
private void ConvertButton_Click(object sender, EventArgs e)
{
Bitmap original = new Bitmap(#"filepath.cover.png");
Bitmap clone = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppPArgb);
using (Graphics conv = Graphics.FromImage(clone))
{
conv.DrawImage(original, new Rectangle(0, 0, clone.Width, clone.Height));
}
}
Hello everyone, i need some help.
I trying to convert PNG or JPEG files to TGA 32bit files and save them, I am still new to programming and could not find an answer here. I found only this code snip here and tried to get him to run, i tried alot of versions to get an output file, but nothing worked, sometimes I got a blank file, sometimes a corrupt one
thank you to everyone who helps me.
Edit:
First of all, thank you, I tried your code and it gives me an empty file. I'm just trying this:
private void TgaConvert_Click(object sender, EventArgs e)
{
TGA original = new TGA(#"file.path.cover.png");
TGA clone = new TGA(original.Width, original.Height, TgaPixelDepth.Bpp32,
TgaImageType.Uncompressed_TrueColor);
using (??? conv = ???(clone))
{
conv.???(original, new ???(0, 0, clone.Width, clone.Height));
clone.Save(#"file.path.cover.tga");
}
}
at the places with "???" I can't get any further
Unfortunately there is no write support for TGA included in the .net framefork.
But there are other open source libraries available. Have a look at TGASharpLib from Zelenskyi Alexandr (https://github.com/ALEXGREENALEX/TGASharpLib).
If a apply his sample to your code, then this is the result:
using TGASharpLib;
...
private void ConvertButton_Click(object sender, EventArgs e)
{
var tga = new TGA(#"filepath.cover.png");
tga.Save(#"filepath.cover.tga");
}
I did it:
using TGASharpLib;
....
TGA T;
private void ConvertButton_Click(object sender, EventArgs e)
{
using (Bitmap original = new Bitmap("file.path.jpg"))
using (Bitmap clone = new Bitmap(original))
using (Bitmap newbmp = clone.Clone(new Rectangle(0, 0, clone.Width, clone.Height), PixelFormat.Format32bppArgb))
T = (TGA)newbmp;
T.Save("file.path.cover.tga");
}
I have trying to deploy tesseract for reading the clipboard image through the code below in a C# window.form. But, a black commandline window appears and nothing happens.
private void b1_Click(object sender, EventArgs e)
{
if (ofd1.ShowDialog() == DialogResult.OK)
{
var img = new Bitmap(ofd1.FileName);
var ocr = new TessBaseAPI("./tessdata", "eng", OcrEngineMode.DEFAULT);
var page = ocr.SetImage(img);
tb1.Text = page.ToString();
}
}
The error it gives is cannot convert from 'System.Drawing.Bitmap' to 'Leptonica.Pix' hope this can be improved.
Instead of creating a bitmap, try using a Pix object like:
var img = Tesseract.Pix.LoadFromFile(ofd1.FileName)
I'm trying to take a picture element from a website and display it inside a PictureBox.. The code doesn't return any errors, but nothing is displayed.
I'm using a WebBrowser class and trying to display the elements using an event that invokes once the webpage is done loading
void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
pictureBox1.ImageLocation = wb.Document.GetElementById("ctl00_mainContent_identityBar_emblemImg").InnerText; // does nothing
label1.Text = "Last Played: " + wb.Document.GetElementById("ctl00_mainContent_lastPlayedLabel").InnerText; // works fine
}
Here's an example of the webpage I'm trying to pull the image from: http://halo.bungie.net/Stats/Halo3/Default.aspx?player=SmitherdxA27
^It's the bird with the orange background on that example.
Pretty easy:
private void Form1_Load(System.Object sender, System.EventArgs e)
{
webBrowser1.Navigate("http://halo.bungie.net/Stats/Halo3/Default.aspx?player=SmitherdxA27");
}
private void WebBrowser1_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
if ((e.Url.ToString() == "http://halo.bungie.net/Stats/Halo3/Default.aspx?player=SmitherdxA27"))
{
HtmlElement elem = webBrowser1.Document.GetElementById("ctl00_mainContent_identityStrip_EmblemCtrl_imgEmblem");
string src = elem.GetAttribute("src");
this.pictureBox1.ImageLocation = src;
}
}
Good luck!
It returns nothing because an image tag doesnt contain inner text. You need to use the the element and generate a bitmap.
public Bitmap GetImage(string id)
{
HtmlElement e = webBrowser1.Document.GetElementById(id);
IHTMLImgElement img = (IHTMLImgElement)e.DomElement;
IHTMLElementRenderFixed render = (IHTMLElementRenderFixed)img;
Bitmap bmp = new Bitmap(e.OffsetRectangle.Width, e.OffsetRectangle.Height);
Graphics g = Graphics.FromImage(bmp);
IntPtr hdc = g.GetHdc();
render.DrawToDC(hdc);
g.ReleaseHdc(hdc);
return bmp;
}