c# how to convert PNG / JPG to TGA 32bit? - c#

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");
}

Related

Moving file selected in listview to another folder with button

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.

Save webbrowser control as image

I'm trying to get webbrowser control as image. But when my code gives me a full screen image. I want to get only webbrowser image. How should I fix my code to get the right image?
Bitmap memoryImage;
private void button2_Click(object sender, EventArgs e)
{
Graphics myGraphics = webBrowser1.CreateGraphics();
Size s = webBrowser1.Size;
memoryImage = new Bitmap(webBrowser1.Width,webBrowser1.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(webBrowser1.Location.X, webBrowser1.Location.Y, 0, 0, s);
memoryImage.Save("C:\\Users\\Koo\\Desktop\\NaverMap.png");
Use the following code - Which supported and does work, but not always works fine.
In some circumstances you will get a blank image screenshot(when there is a more complex html it loads, the more possible it fails)
using (var browser = new System.Windows.Forms.WebBrowser())
{
browser.DocumentCompleted += delegate
{
using (var pic = new Bitmap(browser.Width, browser.Height))
{
browser.DrawToBitmap(pic, new Rectangle(0, 0, pic.Width, pic.Height));
pic.Save(imagePath);
}
};
browser.Navigate(Server.MapPath("~") + htmlPath); //a file or a url
browser.ScrollBarsEnabled = false;
while (browser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}

Using Tesseract OCR in C#

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)

C# WebBrowser Copy Image to Clipboard-full codes

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;

OCR reading using C#

I have a project which is to read character in a captured image but I'm stuck at the button which is to scan image. I ended up tesseract dll in c#, but I don't know how can I code it. I'm a newbie to this programming.
private void Browse_Click(object sender, EventArgs e)
{
//FileInfo fi = new FileInfo(string.Format(#"C:\Documents and Settings\JOrce0201610\My Documents\Visual Studio 2005\Projects\OCR Reader\{0}", imageName));
OpenFileDialog fi = new OpenFileDialog();
fi.InitialDirectory = #"C:\\Documents and Settings\JOrce0201610\My Documents\Visual Studio 2005\Projects\OCR Reader\Card";
fi.Filter = "BMP Image|*.bmp";
fi.FilterIndex = 2;
fi.RestoreDirectory = true;
if (fi.ShowDialog() == DialogResult.OK)
{
//image file path
textBox1.Text = fi.FileName;
//display image in picture box
pictureBox1.Image = new Bitmap(fi.FileName);
}
}
private void Scan_Click(object sender, EventArgs e)
{
Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image
FiltersSequence seq = new FiltersSequence();
seq.Add(Grayscale.CommonAlgorithms.BT709); //First add GrayScaling filter
seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter
temp = seq.Apply(source); // Apply filters on source image
If you are a 'newbie' to programming, OCR is not the best place to start. The best I can suggest is that you use a webservice or existing library that can do this for you.
Microsoft has project Hawaii, Hawaii has an OCR service which is quite easy to use.

Categories

Resources