我正在研究一种绘图程序,但是在绘制橡皮带线时移动鼠标光标时出现闪烁问题.我希望你能帮助我删除那条闪烁的行,这里是代码:
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 GraphicsTest
{
public partial class Form1 : Form
{
int xFirst, yFirst;
Bitmap bm = new Bitmap(1000, 1000);
Graphics bmG;
Graphics xG;
Pen pen = new Pen(Color.Black, 1);
bool draw = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bmG = Graphics.FromImage(bm);
xG = this.CreateGraphics();
bmG.Clear(Color.White);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
xFirst = e.X; …Run Code Online (Sandbox Code Playgroud) 使用一些漂亮的标准C#代码来调整图像大小,并将其放在彩色背景上
Image imgToResize = Image.FromFile(@"Dejeuner.jpg");
Size size = new Size(768, 1024);
Bitmap b = new Bitmap(size.Width, size.Height);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.Green, 0, 0, size.Width, size.Height);
g.DrawImage(imgToResize, new Rectangle(0,150,768, 570));
b.Save("sized_HighQualityBicubic.jpg");
Run Code Online (Sandbox Code Playgroud)
结果在第0和第1列像素中有一个有趣的人工制品.第0列似乎与背景颜色混合,第1列变浅.
看到左上角放大了高质量的双三次和双三次.


..和HighQualityBilinear

这个论坛帖子似乎是有同样问题的人:具有锋利边缘的DrawImage
听起来像是我的错误?我可以理解为什么颜色会在调整大小的图像的顶部混合.但混合左/右边缘的颜色没有意义.有谁知道防止这些文物的修复?
更新:这里的评论非常相似:GDI + InterpolationMode
我想知道Graphics对象绘制一些东西的缓冲区的中间状态.如何获取位图或正在绘制的图像?
我想裁剪并调整图像大小.这是我的代码:
Image image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "Cropper/tests/castle.jpg");
// Crop and resize the image.
Rectangle destination = new Rectangle(0, 0, 200, 120);
Graphics graphic = Graphics.FromImage(image);
graphic.DrawImage(image, destination, int.Parse(X1.Value), int.Parse(Y1.Value), int.Parse(Width.Value), int.Parse(Height.Value), GraphicsUnit.Pixel);
Run Code Online (Sandbox Code Playgroud)
现在我假设我生成的裁剪/调整大小的图像存储在图形对象中.问题是 - 如何将其保存到文件中?
我只是想知道是否有可能在powershell中使用给定的Text创建一个小的,简单的jpg,png,gif:
例如:一个小方块,250px×61px,黄色背景和黑色文字:"测试"
我可以使用"System.Drawing.Image"执行此操作吗?
谢谢
更新
我使用下面的解决方案(从流加载图像),但得到新的问题.img对象绝对正确Image类实例,所有字段都填充了正确的值.但是打电话
img.Save("path/to/new/image.bmp");
Run Code Online (Sandbox Code Playgroud)
在它上面导致GDI +的新异常(System.Runtime.InteropServices.ExternalException,在GDI +接口中) - 我收到错误消息但是很好,我不知道如何翻译它.
原始问题
我有C#.NET Framework 2.0的问题
基本上我试图实现:
Image img = Image.FromFile("Path/To/Image.bmp");
File.Delete("Path/To/Image.bmp"); // Exception, the file is in use!
Run Code Online (Sandbox Code Playgroud)
删除原始文件时,将图像副本保留在内存中非常重要.我虽然有点奇怪,.NET仍然锁定硬盘上的文件,尽管它不再需要任何操作(整个图像现在在内存中,不是吗?)
所以我试过这个解决方案:
Image img = new Image(Image.FromFile("Path/To/Image.bmp")); // Make a copy
// this should immiedietaly destroy original loaded image
File.Delete("Path/To/Image.bmp"); // Still exception: the file is in use!
Run Code Online (Sandbox Code Playgroud)
我可以:
Image img = null;
using(Image imgTmp = Image.FromFile("Path/To/Image.bmp"))
{
img = new Bitmap(imgTmp.Width, imgTmp.Height, imgTmp.PixelFormat);
Graphics gdi = Graphics.FromIage(img);
gdi.DrawImageUnscaled(imgTmp, 0, 0);
gdi.Dispose();
imgTmp.Dispose(); // just …Run Code Online (Sandbox Code Playgroud) 我想"摇动"我的winforms表单以提供用户反馈,就像许多移动操作系统上使用的效果一样.
我可以显然设置窗口的位置和来回Form1.Location.X等,但这种方法的效果很糟糕.我想要一些更流畅的东西 - 或者有没有办法摇动整个屏幕?
我只会使用.net 4.5定位Windows 7.
更新
使用Hans和Vidstige的建议我已经提出了以下内容,当窗口最大化时也可以使用 - 我希望我可以选择两个答案,我已经通过Vidstige投票给你答案,并希望其他人也会这样做.汉斯的回答虽然突破了所有重点.
两种形式MainForm和ShakeForm
Private Sub shakeScreenFeedback()
Dim f As New Shakefrm
Dim b As New Bitmap(Me.Width, Me.Height, PixelFormat.Format32bppArgb)
Me.DrawToBitmap(b, Me.DisplayRectangle)
f.FormBorderStyle = Windows.Forms.FormBorderStyle.None
f.Width = Me.Width
f.Height = Me.Height
f.ShowInTaskbar = False
f.BackgroundImage = b
f.BackgroundImageLayout = ImageLayout.Center
f.Show(Me)
f.Location = New Drawing.Point(Me.Location.X, Me.Location.Y)
'I found putting the shake code in the formLoad event didn't work
f.shake()
f.Close()
b.Dispose()
End Sub
Run Code Online (Sandbox Code Playgroud)
Public Sub shake() …Run Code Online (Sandbox Code Playgroud) 我正在制作一个裁剪图像的程序.我有两个PictureBoxes和一个名为'crop'的按钮.一个图片框包含一个图像,当我在其中选择一个矩形并按"裁剪"时,所选区域出现在另一个图片框中; 所以当我按下裁剪时程序正在运行.问题是:如何将裁剪区域的图像转换为图片框?
Rectangle rectCropArea;
Image srcImage = null;
TargetPicBox.Refresh();
//Prepare a new Bitmap on which the cropped image will be drawn
Bitmap sourceBitmap = new Bitmap(SrcPicBox.Image, SrcPicBox.Width, SrcPicBox.Height);
Graphics g = TargetPicBox.CreateGraphics();
g.DrawImage(sourceBitmap, new Rectangle(0, 0, TargetPicBox.Width, TargetPicBox.Height),
rectCropArea, GraphicsUnit.Pixel);
//Good practice to dispose the System.Drawing objects when not in use.
sourceBitmap.Dispose();
Image x = TargetPicBox.Image;
Run Code Online (Sandbox Code Playgroud)
问题是x = null并且图像显示在图片框中,那么如何将图像从此图片框中转换为Image变量?
我想调整图像大小并将此图像以不同的大小多次保存到文件夹中.我已经尝试过ImageResizer或CoreCompat.System.Drawing,但是这些库与.Net core 2不兼容.我已经搜索了很多关于这个,但我找不到任何合适的解决方案.像在MVC4我用过:
public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null)
{
var versions = new Dictionary<string, string>();
var path = Server.MapPath("~/Images/");
//Define the versions to generate
versions.Add("_small", "maxwidth=600&maxheight=600&format=jpg";);
versions.Add("_medium", "maxwidth=900&maxheight=900&format=jpg");
versions.Add("_large", "maxwidth=1200&maxheight=1200&format=jpg");
//Generate each version
foreach (var suffix in versions.Keys)
{
file.InputStream.Seek(0, SeekOrigin.Begin);
//Let the image builder add the correct extension based on the output file type
ImageBuilder.Current.Build(
new ImageJob(
file.InputStream,
path + file.FileName + suffix,
new Instructions(versions[suffix]),
false,
true));
}
}
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
但是在Asp.Net核心2.0中我被卡住了.我不知道如何在.Net核心2中实现这一点.任何人都可以帮助我.
我正在使用.NET框架(尝试3.5和4.0)加载.TIFF文件并将其另存为.PNG.我希望两次后续调用Save()方法(使用相同的TIFF文件)来生成相同的PNG文件.但是,生成的文件"有时"不同.
下面的C#代码显示了问题:
Image sourceToConvert = Bitmap.FromFile("c:\\tmp\\F1.tif");
sourceToConvert.Save("c:\\tmp\\F1_gen.png", ImageFormat.Png);
for (int i = 0; i < 100; i++)
{
sourceToConvert = Bitmap.FromFile("c:\\tmp\\F1.tif");
sourceToConvert.Save("c:\\tmp\\F1_regen.png", ImageFormat.Png);
if (!CompareFileBytes("c:\\tmp\\F1_gen.png", "c:\\tmp\\F1_regen.png"))
MessageBox.Show("Diff" + i);
}
Run Code Online (Sandbox Code Playgroud)
这将在Windows 64上的迭代8,32,33,73 114,155,196处显示"Diff",而在32位计算机上不显示任何错误.(我使用x86目标;使用x64目标,情况更糟:迭代12,13,14,15 ......的差异)
有没有办法从Save()获得可重现的结果?
可以在此FTP站点上找到示例图像
system.drawing ×10
c# ×7
.net ×5
winforms ×3
graphics ×2
asp.net ×1
bitmap ×1
image ×1
jpeg ×1
powershell ×1
resize-image ×1
vb.net ×1